Async handlers
A handler can await a database query or a web request. While it waits, the other players’ events and commands go on.
This page tells what you can count on while a handler waits, and what to watch.
One player at a time, in order
Section titled “One player at a time, in order”Each player’s events and commands run one after another, in the order they happened: their next event waits until the
handlers of the current one are done. Different players do not wait for each other. JavaScript itself still runs one
thing at a time, so a long calculation (not an await) holds up everyone.
Events that belong to no single player (the world, fires, sirens) run in order among themselves. A hit belongs to the attacker (or the victim, when there is no attacking player), aiming to the aimer, a horn to its driver.
Decisions do not wait in a player’s line: a player who is still logging in after playerConnected can still get a
playerSpawnRequest or playerEnterVehicleRequest. Hold the spawn ({ decision: "hold" }) until they have logged in
if your gamemode needs that.
Keep the PlayerSnapshot, not the ID
Section titled “Keep the PlayerSnapshot, not the ID”A player’s ID is reused: when they leave, the next player to join can get it. So after an await, pass the
PlayerSnapshot you got, not player.id. Every function that targets a player takes
either (a PlayerTarget), and a PlayerSnapshot only ever reaches the connection it came
from. To key your own data by connection, use player.connectionId.
import { players, server } from "nativemp";
server.on("playerConnected", async ({ player }, { signal }) => { const account = await accounts.load(player.name); signal.throwIfAborted(); // stop here if the player left meanwhile sessions.set(player.connectionId, account); await players.setMoney(player, account.balance);});When a player leaves
Section titled “When a player leaves”When a player leaves, the work their events and commands started is cancelled: the
HandlerContext’s signal aborts, their waiting events are dropped, and functions that
target them return { status: "rejected", code: "session-ended" } (or stale-target). Chat and on-screen messages to
them are dropped silently. Then playerDisconnected runs, always, so you can save
their data there.
Cancelling cannot undo what already happened: a query that went through stays done. Check signal.aborted (or call
signal.throwIfAborted()) after an await, before you change your own state.
Timers a handler starts belong to the same player and stop when they leave. For work that belongs to the whole server,
such as a round an admin starts, use server.detached.
What you read while waiting
Section titled “What you read while waiting”players.getById, players.listOnline,
the world functions and the rest always return the newest state, also while your handler waits. The payload your
handler got does not change: after an await it can be older than what players.getById returns.
Two players’ handlers can run at the same time. When one changes state shared between players (a money transfer, a team, a round), make it one step, or use your database’s transactions.
Decisions while waiting
Section titled “Decisions while waiting”A decision still has its deadline while it awaits: answer late and its default applies. See how decisions work.
When your gamemode reloads
Section titled “When your gamemode reloads”A reload cancels everything the old gamemode started. The new one starts with the current state: the players online, the fires burning, the uses going on. Events from before the reload are not sent again.
The old order
Section titled “The old order”Before 0.22 every handler waited for the one before it, for all players. A gamemode written for that can ask for it while it loads:
server.configure({ dispatchMode: "global" });Limits
Section titled “Limits”A handler that takes more than two seconds is logged as slow. When too many handlers are waiting for too long (more than 30 seconds without any finishing), the server restarts your gamemode. One player can have at most 128 events and commands waiting.