To practice for loops, you'll create a lamp that gradually glows brighter and then dims over time. This script can then be applied to any other lights in a project, whether it's a street light or glowing crystal.
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 or model named Lamp.
Select Lamp, and add a light, such as a PointLight or SpotLight. You'll work with the brightness property of the light to create a glowing lamp.
In Lamp, add a new script named GlowScript. In that script, create variables to store the lamp part and the point light.
local lightPart = script.Parentlocal light = lightPart.SpotLightNext, create a variable to hold how much the light's brightness will change each time the loop runs. Then, add another variable for how many seconds the loops will wait before running again.
-- How much the light's brightness will change each timelocal brightnessChange = 1-- How often in seconds the light will change brightnesslocal timeChange = 1
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.
First Loop (Light Increase)
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
- Increment Value: brightnessChange
local timeChange = 1for currentBrightness = 0, 5, brightnessChange doendIn scope of the for loop, set the brightness property of the light to the value in the control variable by typing light.Brightness = currentBrightness. Now, when the loop runs, the light will become brighter.
for currentBrightness = 0, 5, brightnessChange dolight.Brightness = currentBrightnessendSo the change doesn't happen all at once, add a wait function using the value in timeChange.
for currentBrightness = 0, 5, brightnessChange dolight.Brightness = currentBrightnesstask.wait(timeChange)endRun the game to see the light increase in brightness each second.
If you can't see the brightness change over time in the first loop:
- 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.
- Make sure that the first line of your for loop has two total commas separating the control variable, end value, and increment value.
Second Loop (Light Decrease)
To dim the light, use a second for loop. The values of this loop will be reversed so the light starts bright and each second, gets dimmer.
Code 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.
-- Second loop dims lampfor currentBrightness = 5, 0, -brightnessChange dolight.Brightness = currentBrightnesstask.wait(timeChange)endRun your game; you should see the light brighten and then dim.
Making the Light Repeat
Right now, the light only turns on and off once. To make the lamp continuously glow on and off, the for loops will be placed inside a repeating while loop.
Under the for loops, create a new while loop.
while true doendPlace both for loops inside a while loop. Indent the for loops to make them easier to tell apart from the while loop.
while true dofor currentBrightness = 0, 5, brightnessChange dolight.Brightness = currentBrightnesstask.wait(timeChange)endfor currentBrightness = 5, 0, -brightnessChange dolight.Brightness = currentBrightnesstask.wait(timeChange)endendRun the game to see the light turn bright and dim continuously.
Finished Light Script
A finished version of the script can be referenced below.
-- Stores the light attached to this lamplightPart = script.Parentlight = lightPart.SpotLight-- How much the light's brightness will change each timelocal brightnessChange = 1-- How often in seconds the light will change brightnesslocal timeChange = 0.2while true dofor count = 0, 5, brightnessChange dolight.Brightness = counttask.wait(timeChange)end-- Second loop to turn off lightfor count = 5, 0, -brightnessChange dolight.Brightness = counttask.wait(timeChange)endend
Summary
To create complex effects in a project, scripts can use multiple loops, and even combinations of different types of loops. To code a glowing light, two loops are nested within a while loop. As coders add in more loops, be sure to check indentation so code can be easy to read.