Getting started

Tasks and wait

Doing things over time without freezing the game.

A task is a function that can pause. wait(ms) inside a task pauses that task for at least ms milliseconds and lets the game carry on; the task continues on a later tick. The main file itself runs as a task, so it can wait too.

spawn(function()
    while true do
        local me = agents.local_player()
        if me and me:health().percent < 50 then
            combat.cast("Signet of Restoration")
        end
        wait(250)
    end
end)
Starts a task that runs fn(...), and returns its id. The task starts on the next tick.
Pauses the current task for ms milliseconds. Without an argument (or with 0) it waits for the next tick.
Stops a task.

Rules

  • Only tasks can wait: the main file, and functions started with spawn. Callbacks such as on_tick can't; spawn a task from them instead.
  • wait inside a coroutine of your own pauses that coroutine, not the task.
  • Files loaded with require run as part of whatever called it, so a module required from the main file can take several ticks to load, and can wait.
  • All of a plugin's tasks share one time budget each tick. A task that runs long simply pauses and continues on the next tick, and busy tasks take turns. See Limits and errors.