Docs/Lua runtime

How scripts run

How rtv-mp scenario Lua code runs on the server: when it runs, the sandbox, error handling, per-step time budgets, conventions and every limit that applies.

API version 1Lua 5.1Updated 2026-11-07

How scripts run#

One Lua state, on the simulation thread. All scenario code runs on the server's simulation goroutine, between ticks. It never runs at the same time as the parallel area workers, so a script always sees a consistent world, and anything it changes takes effect from the next simulation step. Scripts act on the world only through the same validated server functions the console uses.

Tasks. Every hook handler, chat command, event function, cleanup function and spawned function runs as its own task (a Lua coroutine). A task can pause with wait(seconds) and resume later without blocking the server or other tasks. A task ends when its function returns, when it fails, or when it is cancelled.

When code runs.

TriggerRuns
init.lua and modules required during loadOnce, at load
Hooks caused by the world (players, areas, NPCs, native events)On the simulation tick after the change. Changes are detected by comparing the world once per tick.
Chat commandsImmediately, when the server processes the chat message
/scenario start from the consoleImmediately
Anything started from Lua (spawn, after, hooks fired by a script's own action such as ev:stop(), area:start_event() called by a script)After the code that started it pauses or returns: later in the same tick if the tick's waiting tasks have not run yet, otherwise on the next tick
A task after wait(s)The first tick at or after s seconds

How long a tick lasts depends on how the server is configured, but it is short enough that either is effectively immediate for players. Never rely on two things started from Lua running in the same tick.

Order within one tick. Area hooks (loads, unloads, native events, NPC deaths), then player hooks (leaves, deaths, joins, spawns), then scenario events (ends, then rolls), then waiting tasks that are due. Tasks that are due together resume earliest wake time first, then in the order they were created. Players and areas are visited in ascending ID and key order, so the same situation produces the same order every time.

Errors. A Lua error inside a task ends only that task. The server logs [scenario] error scenario=<name> task="<label>": <message>. The label tells you what failed: player_spawn, player_spawn_point, command /kit, event bounty, event cleanup bounty, spawn, and so on. Other tasks, hooks and the server carry on. If an every() function fails, its loop ends.

Time budget. Each time a task runs, until it finishes or calls wait, it has 25 ms. Time the server spends inside its own calls (a save from give, clear_inventory or teleport) does not count; only Lua execution does. A task that takes longer (for example an endless while true do end) is stopped and logged as stopped after exceeding its 25ms step budget. Long work should be split up with wait().

Sandbox#

Scenarios are trusted operator code. The sandbox prevents mistakes, not deliberate abuse: for example, a script can still allocate a lot of memory.

AvailableRemoved
Base functions (pairs, ipairs, type, tostring, tonumber, pcall, error, select, setmetatable, load, loadstring, unpack, ...)dofile, loadfile, module
table, string, math, coroutineio, os, debug, package, channel
  • print writes to the server log instead of stdout (see print).
  • require loads only .lua files inside the scenario folder (see require).
  • For clock and date values use server.time(), because os is not available.
  • Lua's own coroutine library works inside your code, but wait() must be called from the task itself, not from inside a coroutine you created.

Conventions#

  • Errors. Functions that can fail for normal reasons (an item that doesn't exist, a player who left) return nil, "message". Check the second value:
    local ok, err = player:give("Medkit", 1)
    if not ok then player:tell(err) end
    
    Programming mistakes, such as a wrong argument type, an unknown NPC kind or an empty message, raise a Lua error instead.
  • Handles. Players, areas, NPCs, events and tasks are handles: small references, not copies. Reading a field always returns the current value. When the thing a handle refers to is gone (the player left, the area unloaded, the NPC was removed), identity fields still work, other fields read nil, and methods return nil, "<reason>". Handles can be stored in tables and compared with ==.
  • Positions are tables {x = 1.0, y = 2.0, z = 3.0} in metres. Functions that take a position also accept {x, y, z}.
  • Times are seconds. server.time() and event times (started_at, ends_at) are server wall-clock seconds since 1970 (Unix time, millisecond precision), not in-game time.
  • Names. Command and event names are lowercase. Area names are accepted in any case ("village" or "Village").

Limits#

LimitValue
Loading init.lua and its modules2 s total
One task step (until it finishes or waits)25 ms
Live tasks (spawn refuses beyond this)1024
wait0 to 10,000,000 s
Chat and broadcast messages160 characters
give amount1 to 10,000, and the item's maximum stack (300 for ammunition) per record
Command and event names1 to 32 of a-z 0-9 _
Scenario folder name1 to 64 of A-Z a-z 0-9 _ -
Store key1 to 200 bytes
Store value / all store data256 KB / 4 MB of JSON
Store table nesting32 levels
loot.json add count1 to 20

Found a mistake or missing detail? Tell us in the Discord.