Module 9 · Events and scheduling
Responding to events with :Connect()
Register a callback now so Roblox can run it when an event occurs later.
What you will learn
- Connect a callback to an event
- Explain event-driven timing
- event-driven
- Behavior organized around signals that occur over time.
- connection
- The registration linking an event to a callback.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
A Coin Quest checkpoint should respond when something touches it, not repeatedly search the world every frame.
The mental model
An event is a signal. :Connect(callback) registers behavior and returns immediately. Roblox calls the callback later for each firing.
The callback is passed as a function value; it is not called during connection setup.
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 “colon”: it calls a method with the object supplied as the first receiver 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 part = workspace:WaitForChild("Checkpoint")assert(part:IsA("BasePart"), "Checkpoint must be a BasePart")local function onTouched() print("Checkpoint touched")endpart.Touched:Connect(onTouched)Line by line
- 3
The script obtains its required target.
- 4
The assertion checks the expected class.
- 6
The callback is defined before registration.
- 10
Connect receives the function without calling it.
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 script obtains its required target.
Line 4: The assertion checks the expected class.
Line 6: The callback is defined before registration.
Line 10: Connect receives the function without calling it.
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 callback runs in response to the event rather than during setup.
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
When does onTouched run?
Try it
Mark each step after you have tried it.
Common mistake
part.Touched:Connect(onTouched())Parentheses call the function immediately and pass its return value. Connect needs the callback value onTouched.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- Events announce occurrences.
- Connect registers a callback.
- Roblox calls the callback when the signal fires.
Next: Next, you will use data supplied by an event.
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.