Category
Scheduler
The task library and the legacy wait, spawn and delay globals.
The scheduler runs coroutines cooperatively. A coroutine pauses with task.wait and the scheduler resumes it later, between frames.
task library
task.spawn(f, ...)
Starts f immediately in a new coroutine and passes it ....
fcan be a function or an existing coroutine.- Returns the coroutine.
- If
ferrors, the message is printed as a console error.
task.spawn(function(name)
print("hello", name)
end, "world")
task.defer(f, ...)
Like task.spawn, but f starts later in the same frame, after threads that were waiting have been resumed. Returns the coroutine.
task.wait(t = 0)
Pauses the current coroutine for t seconds of scheduler time and returns how much time actually passed.
- Negative and non-number values count as
0. task.wait()resumes on the next frame.- It can only be used inside a coroutine (the main chunk counts).
local elapsed = task.wait(0.5)
print("waited", elapsed)
task.delay(t, f, ...)
Runs f(...) after t seconds. Returns the coroutine, which you can cancel.
task.delay(3, print, "three seconds later")
task.cancel(co)
Stops a coroutine that is waiting. Safe to call on a finished coroutine.
Legacy globals
These are shortcuts to the same functions.
| Global | Same as |
|---|---|
wait(t) | task.wait(t) |
spawn(f, ...) | task.spawn(f, ...) |
delay(t, f, ...) | task.delay(t, f, ...) |