The task library schedules callbacks and the time library reads clocks.
Tasks run on the game tick. They keep running when the game pauses. Speedhacks change task timing because timers use frame delta.
Types match tools/luau_codegen/extra_bindings/task.dluau.
loadstring is a global, not part of task.
See globals.
type TaskHandle = {
cancel: (self: TaskHandle) -> (),
}
type TaskNamespace = {
spawn: (fn: (...any) -> ...any, ...any) -> (),
delay: (seconds: number, fn: () -> ()) -> TaskHandle,
every: (seconds: number, fn: () -> ()) -> TaskHandle,
defer: (fn: () -> ()) -> TaskHandle,
cancel: (handle: TaskHandle) -> (),
}
type TimeNamespace = {
now: () -> number,
unix: () -> number,
}task.spawn(fn: (...any) -> ...any, ...any) -> ()Runs fn immediately under the callback budget. Extra arguments are passed to fn.
Raises task.spawn requires an initialized runtime when the runtime is not ready.
Errors inside fn are logged. The call returns nothing.
task.spawn(function(name)
print("hello " .. name)
end, "world")task.delay(seconds: number, fn: () -> ()) -> TaskHandleRuns fn once after seconds and returns a handle. A negative value is clamped to zero.
task.every(seconds: number, fn: () -> ()) -> TaskHandleRuns fn repeatedly every seconds and returns a handle.
The interval must be greater than zero, otherwise the call raises an error.
task.defer(fn: () -> ()) -> TaskHandleRuns fn once on the next tick and returns a handle.
task.cancel(handle: TaskHandle) -> ()Cancels a scheduled task. You can also call handle:cancel().
Keep the handle while you expect the callback to run. Dropping it cancels the task when Lua collects the handle userdata.
See Examples.
time.now() -> number
time.unix() -> numbertime.now returns the seconds since the runtime loaded the task library,
using a steady clock, so it suits measuring elapsed time.
time.unix returns the seconds since the unix epoch, using the system clock, so it suits wall clock time.
print(time.now(), time.unix())Tasks use the game scheduler and share the main-thread callback budget. See Getting started and Limits and errors.
tools/luau_codegen/extra_bindings/task.dluausrc/bindings/task/TaskBinding.cppsrc/bindings/task/TaskScheduler.cppsrc/core/Config.hpp