task#
A routine that pauses. Write a sequence as the steps it is, top to bottom, instead of a chain of callbacks.
local task = require("task")task also works without the require, as a ready-made global.
-- a lift: up, wait at the top, down again, forevertask.spawn(function() while true do self:tween({ to = vec(0, 8, 0), seconds = 3 }) task.wait(5) self:tween({ to = vec(0, 0, 0), seconds = 3 }) task.wait(5) endend)The same lift with self:after is four nested functions. task is that, written in order.
spawn#
task.spawn(fn, ...)Starts fn as a routine, passing it the extra arguments. It runs at once, up to its first task.wait; the rest of your code carries on from there.
Called with a dot, not a colon.
wait#
task.wait(seconds?)Pauses the routine it is called from for seconds (0 when you leave it out, which means "the next tick"). Everything else in the world, including your other routines and events, keeps running meanwhile.
It only works inside task.spawn. An event handler is not a routine, so this is an error, on purpose:
self:on_touch(function(player) task.wait(1) -- ❌ task.wait only works inside task.spawnend) self:on_touch(function(player) task.spawn(function() -- ✅ self:set({ light = true }) task.wait(1) self:set({ light = false }) end)end)What it costs#
Each wait is one self:after timer, and each step after a wait runs as that timer's event, with a fresh instruction budget. A routine that loops forever is fine as long as it waits somewhere in the loop.
An error inside a routine stops that routine and shows up in your script's log like any other error. The script, and its other routines, keep running.
Saving the script ends every routine it had running, as it ends its timers.