Glowing Lights with For Loops
Glowing Lights with For Loops


To practice for loops, you’ll create a lamp that gradually glows brighter and then dims over time.
Setting up the Part and Script
The lamp will be a part with an attached light and script.
- To make it easier to see the light, change the game world to night time. In the Explorer > Lighting > change ClockTime to
0
.


- Create a new part named Lamp.
- Select Lamp, and add a PointLight. You’ll work with the brightness property of the PointLight to create a glowing lamp.

- In Lamp, add a new script named GlowScript.
- In GlowScript, create variables to store the lamp part and the point light.
- Next, create a variable to hold how much the light will change each time the loop runs, and a variable for how many seconds the loops will wait before running again.
Using Variables Instead of a Number
Taking the extra step to create a variable for brightness makes it easier to adjust the value later if needed. Coders do this so they can change their variable in one place, without having to go through a potentially long script.
Making the Lamp Glow
The lamp will use two for loops, one that counts up to make the lamp brighter, and one that counts down to dim it. Each for loop will have a control variable called currentBrightness
. That way, as the for loop’s control variable goes up and down, so will the brightness of the light.
Create the First For loop
Remember, a for loop starts with keyword for
followed by a control variable. This script will set the brightness value of the light to value in the control variable.
- Code the for loop using the following values. Remember to separate the control variable and end values with a comma.
- Control variable:
currentBrightness
- Start value: 0
- End value: 5
Values for Brightness
Remember that 0 means the light is off and 5 means it’s on. If you want to try different values, a point light will have values from 0 to 40.
- For the increment value, use the
brightnessChange
variable you just coded. The values of both variables will be added together after the loops runs until the total reaches the end value. - Finish the loop by typing
do
and press Enter to autocomplete theend
of the loop.
For Loops Can Use Numbers or Variables
Variables that have been previously created can be used in place of numbers in for loops for the start, end, and increment values.
- Set the brightness property of the light to the value within the control variable by typing
light.Brightness = brightnessChange
. Now, everytime the loop runs, the light will become brighter.
- So the change doesn’t happen all at once, add a wait function using the value in
timeChange
.
- Run the game to see the light increase in brightness each second.
Troubleshooting Tips
If you can’t see the brightness change over time:
- Make sure that
light.Brightness = currentBrightness
is between thedo
andend
of your for loop. - Check that
timeChange
is at least 1 or higher. Smaller numbers will make the brightness change faster, but be harder to see over time. - Check that the first line of your for loop has two total commas separating the control variable, end value, and increment value.
Turn off the Light
Now that the lamp turns on, you’ll use another for loop to turn the light off. The values of this loop will be reversed so the light starts bright and each second, gets dimmer.
- On your own, try coding a second for loop that decreases the brightness over time. The solution is in the code box below. Use the following values:
- Control Variable -
currentBrightness
set to 5, the end of the last loop. - End - 0, turning off the light.
- Increment - subtract
brightnessChange
Finished Code »
- Run your game; you should see the light brighten and then dim.
Make the Light Repeat
Right now, the light only turns on and off once. To make the lamp continuously glow on and off, the fors loops will be placed inside a repeating while loop.
- Place both for loops inside a while loop. Indent the for loops to make them easier to tell apart from the while loop.
Keep Code Indented
To make your code easier to read, check that your code is indented like the block above. To indent your code:
- Tab to move a line forward.
- Ctrl / Cmd + Tab to move a line back.
- Run the game to see the light turn brighten and dim forever.
Troubleshooting Tips »
If you can’t see the brightness change over time:
- Make sure that
light.Brightness = currentBrightness
is between the do and end of your for loop. - Check that
timeChange
is at least 1 or higher. Smaller numbers will make the brightness change faster, but be harder to see over time. - Check that the first line of your for loop has two total commas separating the control variable, end value, and increment value.


Finished Project Sample
Project File
Download the finished project here.
Finished Script
GlowScript »
Previous Repeating Tasks with For Loops Next Creating a Timed Bridge