UevKF8zjRV_WEtpJsXhr
We use cookies on this site to enhance your user experience

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.

  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.
-- Stores the light attached to parent part
local lightPart = script.Parent
local light = lightPart.PointLight
  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.
-- How much the light's brightness will change each time
local brightnessChange = 1
-- How often in seconds the light will change brightness
local timeChange = 1
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
-- How much the light's brightness will change each time
local brightnessChange = 1
-- How often in seconds the light will change brightness
local timeChange = 1

for currentBrightness = 0, 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.
-- How much the light's brightness will change each time
local brightnessChange = 1
-- How often in seconds the light will change brightness
local timeChange = 1

for currentBrightness = 0, 5, brightnessChange do

end
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.
for currentBrightness = 0, 5, brightnessChange do
    light.Brightness = currentBrightness 
end
  1. So the change doesn’t happen all at once, add a wait function using the value in timeChange.
for currentBrightness = 0, 5, brightnessChange do
    light.Brightness = currentBrightness 
    wait(timeChange)
end
  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
-- First loop brightens lamp
for currentBrightness = 0, 5, brightnessChange do
    light.Brightness = currentBrightness 
    wait(timeChange)
end
 
-- Second loop dims lamp
for currentBrightness = 5, 0, -brightnessChange do
	light.Brightness = currentBrightness 
	wait(timeChange)
end

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

  1. Place both for loops inside a while loop. Indent the for loops to make them easier to tell apart from the while loop.
-- loop that will repeat the on and off for loops	
while true do
	for currentBrightness = 0, 5, brightnessChange do
		light.Brightness = currentBrightness 
		wait(timeChange)
	end

	for currentBrightness  = 5, 0, -brightnessChange do
		light.Brightness = currentBrightness 
		wait(timeChange)
	end

end

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.

  1. Run the game to see the light turn brighten and dim forever.

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.

With the light created, you can copy both GlowScript and PointLight into any other part to make more lamps in your world.

Finished Project Sample

Project File

Download the finished project here.

Finished Script

-- Stores the light attached to this lamp
lightPart = script.Parent
light = lightPart.PointLight

-- How much the light's brightness will change each time
local brightnessChange = 1
-- How often in seconds the light will change brightness
local timeChange = 0.2

while true do 
	for count = 0, 5, brightnessChange do
	    light.Brightness = count
	wait(timeChange)
	end
	
	-- Second loop to turn off light
	for count = 5, 0, -brightnessChange do
		light.Brightness = count 
		wait(timeChange)
	end
end


Previous Repeating Tasks with For Loops Next Creating a Timed Bridge