HyperScript
Docs

← All of the Docs Data and the outside

Async blocks

Guide and examples

Everything in a trigger normally runs on the server thread, where slow code makes the whole server stutter. async: moves a block to a background thread; sync: inside it returns to the server thread.

command /highscores:
    trigger:
        async:
            set {_ranked} to reversed sorted {scores}
            set {_best} to item 1 of {_ranked}
            sync:
                send "&6Best score: &e%{_best}%"
        send "&7Working on it..."
  • The trigger does not wait: in the example, "Working on it..." is sent first.
  • An async: block works on its own copy of the local variables, taken when the block starts. Changes it makes are visible inside the block (including a nested sync:), not after it. Saved variables are shared as usual.
  • You do not have to use sync: just to talk to the server. send, broadcast, execute, open gui and spawn are handed to the server thread automatically when used inside async:. Use sync: when several statements must happen together in one tick.
  • cancel event has no effect inside async: because the event has already finished.
  • What it is for: sorting or filtering large lists, long loops, heavy math. Reading and writing variables is already fast (they are in memory, and storage I/O always happens in the background), so ordinary scripts do not need it.