Syntax
task.delay(1, function()
print("Ready after a scheduling delay")
end)Examples
Run a finite reminder sequence
Script in ServerScriptService. Each reminder yields before the next one, and the loop ends after three. Actual elapsed time can exceed the requested one-second wait.
task.spawn(function()
for remaining = 3, 1, -1 do
print(remaining)
task.wait(1)
end
print("Ready")
end)Use delta time and disconnect
LocalScript in StarterPlayerScripts. This creates a local cosmetic marker and rotates it for three seconds. Multiplying by dt makes the intended angular speed independent of frame count.
local RunService = game:GetService("RunService")
local marker = Instance.new("Part")
marker.Anchored = true
marker.CanCollide = false
marker.Position = Vector3.new(0, 5, 0)
marker.Parent = workspace
local elapsed = 0
local connection: RBXScriptConnection?
connection = RunService.Heartbeat:Connect(function(dt)
elapsed += dt
marker.CFrame *= CFrame.Angles(0, math.rad(90) * dt, 0)
if elapsed >= 3 then
if connection then connection:Disconnect() end
marker:Destroy()
end
end)Best practices
- Do not use a busy loop without yielding; it can stall script execution.
- Cancel delayed work or check its owner before accessing an object that may have been destroyed.
- Use property/event signals for discrete changes, not a Heartbeat poll of every object.
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
Events, connections and cleanup
Connect registers a callback and returns a connection you can disconnect. The callback runs when the event fires, not when it is registered. Long-lived services can keep callbacks alive after a feature has ended, so give each connection a clear cleanup point.Loops and iteration
Choose a numeric for loop for a known range, a table iterator for a collection, or a while loop for a condition. break leaves the loop and continue skips the rest of one iteration. Repeated engine work needs a schedule, not an unbounded busy loop.TweenService and transitions
TweenService interpolates supported properties over time. Creating a tween does not start it; call Play. Completion callbacks can also occur after cancellation, so inspect the playback state before treating the animation as a successful finish.Raycasting and hit results
A raycast asks what a line segment intersects. Its direction vector includes the distance to search, not just a unit direction. A miss returns nil; a hit provides an Instance, position and surface normal. Filters determine what the query ignores.
Connect registers a callback and returns a connection you can disconnect. The callback runs when the event fires, not when it is registered. Long-lived services can keep callbacks alive after a feature has ended, so give each connection a clear cleanup point.Loops and iteration
Choose a numeric for loop for a known range, a table iterator for a collection, or a while loop for a condition. break leaves the loop and continue skips the rest of one iteration. Repeated engine work needs a schedule, not an unbounded busy loop.TweenService and transitions
TweenService interpolates supported properties over time. Creating a tween does not start it; call Play. Completion callbacks can also occur after cancellation, so inspect the playback state before treating the animation as a successful finish.Raycasting and hit results
A raycast asks what a line segment intersects. Its direction vector includes the distance to search, not just a unit direction. A miss returns nil; a hit provides an Instance, position and surface normal. Filters determine what the query ignores.