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

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