Task schedules work and yields safely.
Available as the global task.
Prefer task.wait over bare wait. Task can yield inside Event listeners.
Overview
Methods
- .wait(seconds: number?) : number
- .spawn(fn: function, ...) : thread
- .delay(seconds: number, fn: function, ...) : thread
- .defer(fn: function, ...) : thread
- .cancel(thread: thread)
API
Methods
wait
wait(seconds: number?) : number
Yields the current thread for seconds (default 0).
Returns the actual time waited.
part.Touched:Connect(function(player)
task.wait(0.5)
player.Character:Kill()
end)
Bare global wait remains for compatibility. Prefer task.wait.
spawn
spawn(fn: function, ...) : thread
Runs fn on a new thread as soon as possible.
Extra args are passed to fn.
task.spawn(function(name)
print("Hello", name)
end, "world")
delay
delay(seconds: number, fn: function, ...) : thread
Runs fn after seconds.
Returns the scheduled thread so you can cancel it.
local t = task.delay(3, function()
UI:Hud("Go!")
end)
defer
defer(fn: function, ...) : thread
Runs fn after the current thread yields or finishes, on a new thread.
cancel
cancel(thread: thread) : void
Cancels a thread created by spawn, delay, or defer.
Does nothing if the thread already finished.
