Glowing Lights with For Loops

15 min

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.

  1. To make it easier to see the light, change the game world to night time. In the Explorer > Lighting > change ClockTime to 0.
  1. Create a new part named Lamp.
  2. Select Lamp, and add a PointLight. You’ll work with the brightness property of the PointLight to create a glowing lamp.
  1. In Lamp, add a new script named GlowScript.
  2. In GlowScript, create variables to store the lamp part and the point light.
  1. 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.

  1. 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.

  1. 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.
  2. Finish the loop by typing do and press Enter to autocomplete the end 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.

  1. 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.
  1. So the change doesn’t happen all at once, add a wait function using the value in timeChange.
  1. 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 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.

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.

  1. 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

  1. Run your game; you should see the light brighten and then dim.