Roblox Studio · Lesson 6

Cycle and rhythm: light up the countdown signal light

Use for and task.wait() to create a three-second countdown, and then compare the finite loop and the continuously running loop.

10–15 years old 60 minutes for / task.wait Hands-on practice Testing and Saving

Course overview

Lets the lights work at rhythm, without copying the code ten times.

The expedition team is ready to go: 3, 2, 1, the green light is on! This little countdown is just right for the cycle.

You will first write a for loop that can count the number of times, and then add wait and color to it. Each run should be able to tell: how many times it was executed and where it ended up.

01 · BEFORE

Before starting

Will use numeric variables, if and Output. Create a new independent Baseplate and save it as Lesson06.

02 · MAKE

Your work

A signal light that counts down for three seconds, lights green for two seconds, and finally resumes standby.

03 · CHECK

Complete flag

Output displays 3, 2, 1, GO in sequence; the signal light returns to its original color, and the program will not run endlessly.

Screenshot of the lamp body of the official cycling light practice
Figure 6-1: Official light body reference: a simple Part can also become a signal light; this lesson names it Signal.Roblox Creator Documentation · CC BY 4.0 · The original image has not been modified · Description of the permit

Learning route

Prepare first, then do it; after seeing the result, remember to save it.

Every time a small step is completed, it is tested. Say what you expect to see first, and then use Play and Output to check it. Don't rush to write all the code at once.

1

01·Put the signal light in front of your eyes

Make the scene simple enough before you can judge whether the change is caused by the code. The signal light doesn't have to be fancy, but it needs to be visible from the spawn point.

2

02·Understand the starting point, end point and step size of the cycle

for remaining = 3, 1, -1 means starting from 3, subtracting 1 each time until reaching 1. The end point will be included when it matches the step direction, so it is executed three times here.

3

03 · Connect the countdown to color

Now think of the loop body as a "beat". Each beat prints a number, turns orange, and waits one second. The green light part is executed only after the loop ends.

4

04 · How to choose between for and while

If you already know how many times you want to repeat it, use for first. If you need to "continue as long as the condition is true", you can use while, but you must explain how the condition ends.

5

05·Use frequency and rhythm positioning issues

The numbers are flushed instantly, usually because task.wait is missing from the loop body. There is no output at all. First check whether the starting point, end point and step size match.

Step by step

01·Put the signal light in front of your eyes

Make the scene simple enough before you can judge whether the change is caused by the code. The signal light doesn't have to be fancy, but it needs to be visible from the spawn point.

This lesson uses Color and Material for visual feedback. The Neon material will appear to be shiny, but it will not illuminate nearby objects; actual lighting will be learned in Lesson 14.

  1. Create Part in Workspace, name it Signal, set Size to 4, 4, 1, and Position to 0, 5, -12.
  2. Check Anchored and set Material to Neon.
  3. Create a new Countdown in ServerScriptService and open Output.

Step by step

02·Understand the starting point, end point and step size of the cycle

for remaining = 3, 1, -1 means starting from 3, subtracting 1 each time until reaching 1. The end point will be included when it matches the step direction, so it is executed three times here.

task.wait(1) Let the current script continue later, and the game can still handle other things. It is not a high-precision stopwatch and the actual wait may be slightly longer than one second.

  1. Enter a short code that predicts the three numbers that will appear.
  2. Observe the rhythm of Output after Play.
  3. After Stop, change the step size to -2, and predict and verify that the result is 3, 1.
ServerScriptService > Countdown · Loop experimentLuau
for remaining = 3, 1, -1 do
    print(remaining)
    task.wait(1)
end
print("GO!")
Official interface reference for observing loop printing in Output
Figure 6-2: Official Output interface reference: When running the countdown, check 3, 2, and 1 in order to determine whether the waiting time is effective.Roblox Creator Documentation · CC BY 4.0 · The original image has not been modified · Description of the permit

Step by step

03 · Connect the countdown to color

Now think of the loop body as a "beat". Each beat prints a number, turns orange, and waits one second. The green light part is executed only after the loop ends.

The full script only runs one round so you can easily tell where it ends. When you need to retry, Stop and then Play instead of appending the same code to the end of the script.

  1. Replace the entire contents of Countdown.
  2. Play: Orange lasts for about three seconds, green lasts for about two seconds, and finally returns to blue.
  3. Change the number 3 to 5 and check whether the countdown has been increased by two beats.
ServerScriptService > Countdown · Complete scriptLuau
local signal = workspace:WaitForChild("Signal")
local READY = Color3.fromRGB(55, 140, 220)
local COUNTING = Color3.fromRGB(255, 190, 65)
local GO = Color3.fromRGB(80, 220, 130)

signal.Color = READY
task.wait(2)
for remaining = 3, 1, -1 do
    signal.Color = COUNTING
    print("Starting in", remaining)
    task.wait(1)
end
signal.Color = GO
print("GO!")
task.wait(2)
signal.Color = READY
print("Countdown finished")

Step by step

04 · How to choose between for and while

If you already know how many times you want to repeat it, use for first. If you need to "continue as long as the condition is true", you can use while, but you must explain how the condition ends.

Below is a separate five-blink version that replaces the previous countdown. There is a wait for each on and off; if you forget to add flashes, the condition may always be true.

  1. Keep a copy of the countdown version first, and then replace Countdown with the following code.
  2. Count if it flashes exactly five times.
  3. Do not try running an infinite loop without waiting; it may prevent Studio from responding in a timely manner.
Countdown · Optional replacement version, not superimposed with the previous paragraphLuau
local signal = workspace:WaitForChild("Signal")
local flashes = 0
while flashes < 5 do
    signal.Transparency = 0
    task.wait(0.4)
    signal.Transparency = 0.75
    task.wait(0.4)
    flashes += 1
end
signal.Transparency = 0
print("Flashes:", flashes)

Step by step

05·Use frequency and rhythm positioning issues

The numbers are flushed instantly, usually because task.wait is missing from the loop body. There is no output at all. First check whether the starting point, end point and step size match.

The loop variable remaining only belongs inside the loop. If you want to retain the statistical results after the loop ends, you should first create variables outside like flashes.

  1. Test 1 second and 0.5 second waits to describe tempo differences.
  2. Confirm that each do has a corresponding end.
  3. Keep the version you like better, Stop and save.

Homework

It's your turn to change and make your own version.

1 Departure in five seconds

Change countdown to 5 to 1, green light for 3 seconds.

2 Quiet signal light

Allows the program to express its state through different colours and final text without relying on sound.

3 Selected: two rounds of signal

Load the complete round into the function and call it twice with for, leaving two seconds to rest in between.

Completion criteria:First verify the work in Playtest, then click Stop and save it as "Initials_Lesson06" or the corresponding Homework copy; do not overwrite the runnable version of the previous lesson.

Exit questions

Answer it yourself first, and then expand on it.

How many times is for i = 1, 4 do executed?

Four times, i is 1, 2, 3, 4 in sequence.

Why does the countdown need -1?

It is the step size of each decrease, ensuring that the number advances towards the end point 1.

Will task.wait pause the entire game?

No, only the current execution thread is made to wait; other scripts and games can still run.

Who guarantees the exit condition of while?

Guaranteed by your code. Have the condition eventually become false, or break on an explicit condition.

Explore further

This is where we start when we need to check the data.

The text code can be copied directly. First confirm the script type, placement, and whether this code is a complete version or a small experiment; do not run multiple versions of the same script superimposed. The source of the interface reference picture has been marked. The object names in the picture may be different from those in this lesson. Please refer to the operation steps.

Download the code and placement instructions for this lesson