Module 9 · Events and scheduling
Connections and cleanup
Store RBXScriptConnection values and disconnect listeners when their owner ends.
What you will learn
- Store an RBXScriptConnection
- Disconnect it safely
- Avoid duplicate listeners
- RBXScriptConnection
- The handle returned by Connect for controlling an event listener.
- cleanup
- Releasing connections, tasks, or temporary objects no longer needed.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
Reinitializing a Coin Quest controller without cleanup can make one touch run the same behavior several times.
The mental model
The connection is a resource owned by the controller that created it. Disconnecting stops future callbacks but does not undo work already completed.
Make teardown idempotent: calling it again should not create an error or a new listener.
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.
if is read as “if”: it starts a decision by evaluating a condition.
then is read as “then”: it marks the body that can run when the condition is truthy.
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 “optional”: it marks a type that may also be nil.
A tiny example
--!strictlocal part = script.Parentassert(part:IsA("BasePart"), "Script must be under a BasePart")local connection: RBXScriptConnection? = part.Touched:Connect(function() print("Touched")end)local function cleanup() if connection then connection:Disconnect() connection = nil endendtask.delay(10, cleanup)Line by line
- 6
The optional typed local owns the returned connection.
- 10
cleanup names the teardown behavior.
- 11
The guard permits repeated cleanup calls.
- 12
Disconnect stops future callback delivery.
- 13
Clearing the reference records that cleanup happened.
- 17
task.delay schedules teardown without blocking setup.
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 6: The optional typed local owns the returned connection.
Line 10: cleanup names the teardown behavior.
Line 11: The guard permits repeated cleanup calls.
Line 12: Disconnect stops future callback delivery.
Line 13: Clearing the reference records that cleanup happened.
Line 17: task.delay schedules teardown without blocking setup.
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 listener stops once and repeated teardown remains harmless.
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
What happens to touches after cleanup runs?
Try it
Mark each step after you have tried it.
Common mistake
Connecting again every time a round begins without disconnecting the old listener produces duplicate behavior. Give the connection the same lifecycle as its controller.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- Connections are owned resources.
- Disconnect ends future callbacks.
- Idempotent cleanup prevents duplicate teardown errors.
Next: Next, you will schedule delayed and concurrent work with the task library.
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.