task¶
Library · task · server and client scripts
task runs functions later or in their own threads, and pauses the running thread. Scheduled threads belong to the calling script: the engine drops them if the script reloads or its entity is destroyed. Waits and delays count update ticks, so they end on the first tick at or after the time.
cancel¶
task.cancel stops a thread from task.delay, task.defer or task.spawn so it never resumes.
Example
defer¶
task.defer runs a function at the end of this tick, after every Update, and returns its thread.
delay¶
task.delay runs a function after seconds, and returns its thread. seconds must be a finite non-negative number.
Example
spawn¶
task.spawn runs a function at once, until it waits or ends, and returns its thread. An error in the function logs a Task error warning and does not stop the caller.
Example
wait¶
task.wait pauses the running thread for at least seconds, or one tick when seconds is left out, and returns the seconds that passed. seconds must be a finite non-negative number when given.
Effects
Update,PhysicsUpdateandWorld.Systemcallbacks cannot wait; start the waiting code withtask.spawninstead.- A script finishes loading at its first top-level wait, so declare
UpdateandPhysicsUpdatebefore it; seeUpdate.