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
- 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:
- 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
brightnessChangevariable 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
doand press Enter to autocomplete the
endof 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
- Run the game to see the light increase in brightness each second.
If you can’t see the brightness change over time:
- Make sure that
light.Brightness = currentBrightnessis between the
endof your for loop.
- Check that
timeChangeis 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 -
currentBrightnessset to 5, the end of the last loop.
- End - 0, turning off the light.
- Increment - subtract
- Run your game; you should see the light brighten and then dim.