Module 5 · Repeating work with loops
Mini-project: A round countdown
Count down with status messages and safe timing instead of busy waiting.
What you will learn
- Count down with a negative numeric step
- Yield between updates with task.wait
- Provide start and finish status
- yield
- Pause this thread so other scheduled work can run.
- busy waiting
- Repeatedly checking without an appropriate yield or event.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
A visible round countdown gives Coin Quest structure, but its timing must let the rest of the simulation keep running.
The mental model
Use a bounded numeric loop for known seconds. Each iteration reports the state, then task.wait yields before the next one.
The wait duration is approximate scheduling, not precision physics timing. The important beginner rule is that the loop is finite and yields.
New words and symbols
--!strict asks Studio to check your code for type mistakes. It is an instruction for the editor and produces no Output message.
Matching quotes such as "hello" surround a string: a piece of text. The quotation marks tell Luau where the text starts and ends.
print(...) calls the built-in print function. Put the values to show inside its parentheses; separate multiple values with commas. Read the result in Studio's Output panel.
local is read as “local”: it creates a name that is available only in the current code scope.
end is read as “end”: it closes a block such as a decision, loop, or function.
for is read as “for”: it starts a controlled repetition with a changing loop value.
= is read as “is assigned”: it evaluates the right side and stores that value in the name on the left.
. is read as “dot”: it accesses a named property or table field.
A tiny example
--!strictlocal roundSeconds = 5print("Round begins")for secondsLeft = roundSeconds, 1, -1 do print(`{secondsLeft} seconds left`) task.wait(1)endprint("Round finished")Line by line
- 3
The duration has a descriptive name and unit.
- 4
A start status appears once.
- 6
The loop counts from five through one with a negative step.
- 7
Interpolation formats the current state.
- 8
task.wait yields between updates.
- 11
The completion status runs after the finite loop.
Read the code aloud
Read one statement at a time: name the value being read, what the operation does, and where its result goes. Keep identifiers exactly as written so you can find them again in the code.
Pause at calls, conditions, and loops. Explain when the body runs before moving to the next statement; use the line notes above when a symbol is unfamiliar.
Execution trace
Begin at the first executable statement. Comments and blank lines do not run; a branch chooses which block runs, and a loop may revisit its body.
Line 3: The duration has a descriptive name and unit.
Line 4: A start status appears once.
Line 6: The loop counts from five through one with a negative step.
Line 7: Interpolation formats the current state.
Line 8: task.wait yields between updates.
Line 11: The completion status runs after the finite loop.
State before and after
Before running, identify the starting values and any existing Studio objects this example needs. List what you expect to change.
After your practice change, check this result: The finite countdown yields between updates and ends with one completion message.
Reading or printing a value leaves it unchanged. Look for assignments, calls that change state, and property updates to explain each change you observe.
Predict before running
Which numbered message appears last?
Try it
Mark each step after you have tried it.
Common mistake
while true do print("Still running")endThis endless body never yields and can freeze useful execution. A known countdown belongs in a bounded loop with task.wait.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- A negative step creates a countdown.
- task.wait yields scheduled work.
- Start and finish messages belong outside the repeated body.
Next: Module 6 packages repeated behavior into functions with inputs and results.
Studio challenge
A round countdown
Checks are formative feedback, not cheat-proof certification. You can finish this lesson without the plugin.
- Target
- ServerScriptService/LearningLuau/RoundCountdown
- Check mode
- source and output
Challenge state: idle
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.