task
Summary
Functions
desynchronize():() |
synchronize():() |
API Reference
Functions
spawn
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)) --> deaddefer
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
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
cancel
Parameters
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