@usbx/task
Waits, delays and threads
local task = require("@usbx/task")
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
Use it forpausing between rounds, or counting down the seconds before a race starts.
Runs a function in a new thread now, with the arguments.
task.spawn<A...>(callback: (A...) -> (), A...) -> thread
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.
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.
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.
Runs a function every interval of seconds, the first time one interval after the call. Missed intervals are skipped.
task.every(seconds: number, callback: () -> ()) ->
Connectionexample
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.
Stops a thread before it runs again.
task.cancel(thread: thread) -> ()
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.
The script clock in seconds. time.now is the wall clock instead.
task.now() -> number
Use it fortiming how long a player takes to finish an obby, by comparing the clock at the start and the end.