Open Asterune
task

task

Schedules, yields, and cancels script threads.

A table in every script's environment.

Behavior

task schedules and controls script threads. task.spawn starts a function on its own thread immediately, task.defer resumes it after the current scheduler pass, task.delay runs it after a delay, and task.wait pauses the current thread. task.cancel removes a scheduled thread, including a thread parked in a wait. A suspended thread that is not scheduled produces an error.

The older global spawn, delay, and wait functions remain for compatibility, but their timing and arguments differ. Prefer the task forms when writing new code.

Fields

cancel · defer · delay · desynchronize · spawn · synchronize · wait

cancel

task.cancel(thread) removes a scheduled thread, including one parked in a wait. An unscheduled suspended thread causes an error.

defer

task.defer(functionOrThread, ...) schedules work after the current scheduler pass and returns the thread.

delay

task.delay(seconds, functionOrThread, ...) schedules work after the supplied delay and returns the thread. The scheduler determines the actual resume time.

desynchronize

task.desynchronize() yields serial execution and resumes in the parallel phase. Requires a parallel ExecutionGroup. Calling it while already parallel has no effect.

spawn

task.spawn(functionOrThread, ...) immediately starts or resumes a scheduled thread with the supplied arguments and returns the thread.

synchronize

task.synchronize() yields parallel execution and resumes in the serial phase. Calling it while already serial has no effect.

wait

task.wait(seconds?) yields the current thread until the delay has elapsed and returns the elapsed time in seconds.

Example

Luau
task.defer(function()
  print("after the current handler")
end)

local later = task.delay(5, function()
  print("this will not run")
end)
task.cancel(later)

Instance · Debris