Blinking Light
Part of the Roblox Game Scripting section of Coddy's Lua journey. Lesson 9 of 25.
Change a part twice in a row and you only ever see the second change: the script runs both lines in an instant. task.wait(seconds) pauses the script in between, while the rest of the game keeps running:
print("Ready")
task.wait(3)
print("Go!")Go! appears 3 seconds after Ready.
To repeat something forever, use a while true do loop. It runs its body, then starts over:
local sign = script.Parent
while true do
sign.Color = Color3.fromRGB(255, 0, 0)
task.wait(0.5)
sign.Color = Color3.fromRGB(0, 0, 255)
task.wait(0.5)
endThis sign flashes red and blue every half second. A while true loop must contain a task.wait: without one it never lets the game continue, and Roblox stops the script with a timeout error.
Challenge
EasyMake the lamp blink: the script switches it so fast that you never see it glow.
- Add
task.wait(1)after theNeonline and after thePlasticline. - Wrap all four lines in
while true do ... end.
Press Play: the lamp glows for 1 second, then goes dark for 1 second, over and over. Then Check.
Try it yourself
local lamp = script.Parent
lamp.Material = Enum.Material.Neon
lamp.Material = Enum.Material.Plastic
All lessons in Roblox Game Scripting
2Loops and Functions
Row of PillarsStaircase BuilderTower BuilderCoin FactoryBlinking LightVanishing Bridge5Your First Game
Your First ObbyPractice on your own: Online Lua compiler