Skip to content
Untitled Sandbox - Developer Documentation
docs/api/task

@usbx/task

Waits, delays and threads

local task = require("@usbx/task")

task.sleep

functionwaits

Yields for a time in seconds and returns the time that passed. A negative time is an error, and so is a wait where the code cannot yield.

task.sleep(seconds: number) -> number
example
task.sleep(1)

Use it forpausing between rounds, or counting down the seconds before a race starts.

secondsHow long to wait.

task.spawn

function

Runs a function in a new thread now, with the arguments.

task.spawn<A...>(callback: (A...) -> (), A...) -> thread
example
task.spawn(function() print("now") end)

Use it forstarting a looping light show that runs on its own, without stopping the rest of your script.

callbackThe function to run.
...optionalArguments for the function.

task.defer

function

Runs a function in a new thread later in this tick.

task.defer<A...>(callback: (A...) -> (), A...) -> thread
example
task.defer(function() print("later this tick") end)

Use it forrunning a follow-up step later in the same tick, after the rest of your setup code.

callbackThe function to run.
...optionalArguments for the function.

task.delay

function

Runs a function in a new thread after a time in seconds.

task.delay<A...>(seconds: number, callback: (A...) -> (), A...) -> thread
example
task.delay(3, function() print("3 seconds later") end)

Use it forclosing a door a few seconds after it opens, or anything else that should happen a little later.

secondsHow long to wait first.
callbackThe function to run.
...optionalArguments for the function.

task.every

function

Runs a function every interval of seconds, the first time one interval after the call. Missed intervals are skipped.

task.every(seconds: number, callback: () -> ()) -> Connection
example
task.every(5, function() print("tick") end)

Use it forgiving every player tokens each minute, or sending in a new enemy wave on a steady timer.

secondsThe interval.
callbackThe function to run at each interval.

task.cancel

function

Stops a thread before it runs again.

task.cancel(thread: thread) -> ()
example
local timer = task.delay(10, function() print("time is up") end) task.cancel(timer)

Use it forstopping a round timer early when everyone has finished, so it does not go off.

threadA thread from task.spawn, task.defer or task.delay.

task.now

function

The script clock in seconds. time.now is the wall clock instead.

task.now() -> number
example
local started = task.now()

Use it fortiming how long a player takes to finish an obby, by comparing the clock at the start and the end.

↑ ↓ moveEnter openEsc close