Module 9 · Events and scheduling
Scheduling with the task library
Choose task.wait, task.delay, or task.spawn according to timing intent.
What you will learn
- Distinguish three modern task APIs
- Avoid legacy scheduling functions
- scheduler
- The runtime system that resumes threads and callbacks over time.
- deferred work
- Behavior arranged to run later rather than blocking current setup.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
Coin Quest needs delays for temporary effects and independent work without using deprecated timing helpers.
The mental model
task.wait yields the current thread. task.delay schedules a callback after time. task.spawn schedules a callback to run independently soon.
Choose the narrowest tool that matches intent, and keep ownership or cancellation needs visible.
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.
. is read as “dot”: it accesses a named property or table field.
A tiny example
--!strictlocal function announceLater() print("The gate is ready")endtask.delay(2, announceLater)task.spawn(function() print("Preparing Coin Quest")end)print("Setup continues")Line by line
- 3
The delayed callback is named.
- 7
task.delay schedules it without yielding setup.
- 8
task.spawn schedules separate immediate work.
- 12
The current thread continues after scheduling.
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
Start with the statements outside function bodies. A function definition creates a callable value; its body runs when a call, event, or scheduled task reaches it.
Trace the arguments at each call and the branch or loop inside that call. Return to the caller when the function returns. For an event, use the action named in Try it to trigger the callback.
Line 3: The delayed callback is named.
Line 7: task.delay schedules it without yielding setup.
Line 8: task.spawn schedules separate immediate work.
Line 12: The current thread continues after scheduling.
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: Each scheduling choice has an explicit timing purpose and no legacy helper is used.
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 call schedules work after about two seconds?
Try it
Mark each step after you have tried it.
Common mistake
Do not use older global timing helpers in recommended code. The task library has clearer modern scheduling behavior.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- task.wait yields current work.
- task.delay schedules later work.
- task.spawn schedules separate work soon.
Next: Next, you will handle players who join, respawn, or already exist.
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.