Syntax
local connection = signal:Connect(function()
print("Changed")
end)
connection:Disconnect()Examples
Watch one property
Script in ServerScriptService. This self-contained example watches a part’s color, changes it and disconnects before destroying the part. The callback reads the new property value.
local part = Instance.new("Part")
part.Anchored = true
part.Parent = workspace
local connection = part:GetPropertyChangedSignal("Color"):Connect(function()
print("Color:", part.Color)
end)
part.Color = Color3.fromRGB(200, 90, 50)
task.delay(2, function()
connection:Disconnect()
part:Destroy()
end)Stop listening to a long-lived service
LocalScript in StarterPlayerScripts. This counts five seconds of client frames, then disconnects. A service such as RunService outlives a temporary UI or character feature.
local RunService = game:GetService("RunService")
local elapsed = 0
local connection: RBXScriptConnection?
connection = RunService.Heartbeat:Connect(function(dt)
elapsed += dt
if elapsed >= 5 then
if connection then connection:Disconnect() end
print("Finished watching")
end
end)Best practices
- Store connections when you need to stop callbacks before their signal owner is destroyed.
- Connect a function rather than calling it inside Connect.
- Do not install the same listener every frame or on every button press; repeated connections multiply actions.
At a glance
- Purpose
- Typed scripting and Roblox development
- File extension
- .luau
- Runs in
- Luau host; Roblox engine examples require Roblox Studio
- Usually used with
- Roblox APIs and Studio
Specifications & further reading
Related Luau documentation
task scheduling and RunService
Use task scheduling for delayed or deferred work, and RunService when a feature genuinely depends on simulation or rendering steps. A delay is a minimum scheduling interval rather than an exact clock. Avoid doing work every frame when an event can report the change.Player lifecycle and character spawning
A Player can remain connected while its character is destroyed and replaced. Initialize player state once, and character state on every spawn. Connecting an event alone can miss objects that already exist when a script starts.Closures and captured state
A closure is a function that keeps access to locals from its surrounding scope. Each call to a factory can create independent state. Capturing a value also extends how long it stays reachable, which matters for long-lived callbacks.Script, LocalScript and ModuleScript placement
The same code can behave differently depending on where it runs. These lessons use server Scripts in ServerScriptService and LocalScripts in StarterPlayerScripts or StarterGui. A ModuleScript runs when required. Script RunContext also affects execution, so inspect it when a script appears silent.
Use task scheduling for delayed or deferred work, and RunService when a feature genuinely depends on simulation or rendering steps. A delay is a minimum scheduling interval rather than an exact clock. Avoid doing work every frame when an event can report the change.Player lifecycle and character spawning
A Player can remain connected while its character is destroyed and replaced. Initialize player state once, and character state on every spawn. Connecting an event alone can miss objects that already exist when a script starts.Closures and captured state
A closure is a function that keeps access to locals from its surrounding scope. Each call to a factory can create independent state. Capturing a value also extends how long it stays reachable, which matters for long-lived callbacks.Script, LocalScript and ModuleScript placement
The same code can behave differently depending on where it runs. These lessons use server Scripts in ServerScriptService and LocalScripts in StarterPlayerScripts or StarterGui. A ModuleScript runs when required. Script RunContext also affects execution, so inspect it when a script appears silent.