task

Summary

Functions
spawn(functionOrThread: function | coroutine,...: Variant):coroutine
defer(functionOrThread: function | coroutine,...: Variant):coroutine
delay(duration: number,functionOrThread: function | coroutine,...: Variant):coroutine
wait(duration: number):number
cancel(thread: coroutine):()

API Reference
Functions
spawn
task.spawn(
functionOrThread:function | coroutine, ...:Variant
Parameters
functionOrThread:function | coroutine
...:Variant
Returns
Code Samples
Task Spawn
local function greet(name, punctuation)
print("Hello, " .. name .. punctuation)
end
print("before") --> before
task.spawn(greet, "Roblox", "!") --> Hello, Roblox!
print("after") --> after
-- A suspended coroutine can be resumed with arguments too.
local worker = coroutine.create(function(label)
print("worker started with", label)
end)
task.spawn(worker, "job 1") --> worker started with job 1
print(coroutine.status(worker)) --> dead

defer
task.defer(
functionOrThread:function | coroutine, ...:Variant
Parameters
functionOrThread:function | coroutine
...:Variant
Returns
Code Samples
Task Defer
print("one") --> one
task.defer(function(message)
print(message) --> deferred
end, "deferred")
task.spawn(function()
print("two") --> two
end)
print("three") --> three
-- The deferred function runs after the current code has finished, so it prints last.

delay
task.delay(
duration:number, functionOrThread:function | coroutine, ...:Variant
Parameters
duration:number
functionOrThread:function | coroutine
...:Variant
Returns
Code Samples
Task Delay
local scheduledAt = os.clock()
task.delay(1, function(message)
local elapsed = os.clock() - scheduledAt
print(message, elapsed >= 1) --> Delayed hello true
end, "Delayed hello")
print("scheduled") --> scheduled
-- The returned thread can be cancelled before it runs.
local cancelled = task.delay(2, function()
print("never printed")
end)
task.cancel(cancelled)

desynchronize
task.desynchronize():()
Returns
()
Code Samples
Task Desynchronize
-- Inside a Script that is a descendant of an Actor, desynchronize() resumes the
-- thread in the next parallel phase, where heavy work can run alongside other Actors.
-- Outside of an Actor it raises an error, which pcall lets us observe.
local ok = pcall(task.desynchronize)
print(ok) --> false (when this script is not a descendant of an Actor)

synchronize
task.synchronize():()
Returns
()
Code Samples
Task Synchronize
-- Inside a Script that is a descendant of an Actor, synchronize() resumes the
-- thread in the next serial phase, where it is safe to modify the data model.
-- Outside of an Actor it raises an error, which pcall lets us observe.
local ok = pcall(task.synchronize)
print(ok) --> false (when this script is not a descendant of an Actor)

wait
task.wait(duration:number):number
Parameters
duration:number
Default Value: 0
Returns
Code Samples
Task Wait
local elapsed = task.wait(1)
print(elapsed >= 1) --> true
-- With no argument the thread resumes on the next Heartbeat.
local frameTime = task.wait()
print(frameTime < 1) --> true

cancel
task.cancel(thread:coroutine):()
Parameters
thread:coroutine
Returns
()
Code Samples
Task Cancel
local thread = task.delay(5, function()
print("Hello world!") -- never printed
end)
task.cancel(thread)
print(coroutine.status(thread)) --> dead
-- Cancelling the currently running thread is not allowed and raises an error.
local ok = pcall(task.cancel, coroutine.running())
print(ok) --> false

©2026 Roblox Corporation. Roblox, the Roblox logo and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.