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.
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.
| Trigger | Runs |
|---|---|
init.lua and modules required during load | Once, 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 commands | Immediately, when the server processes the chat message |
/scenario start from the console | Immediately |
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.
| Available | Removed |
|---|---|
Base functions (pairs, ipairs, type, tostring, tonumber, pcall, error, select, setmetatable, load, loadstring, unpack, ...) | dofile, loadfile, module |
table, string, math, coroutine | io, os, debug, package, channel |
printwrites to the server log instead of stdout (seeprint).requireloads only.luafiles inside the scenario folder (seerequire).- For clock and date values use
server.time(), becauseosis not available. - Lua's own
coroutinelibrary works inside your code, butwait()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:Programming mistakes, such as a wrong argument type, an unknown NPC kind or an empty message, raise a Lua error instead.local ok, err = player:give("Medkit", 1) if not ok then player:tell(err) end - 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 returnnil, "<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#
| Limit | Value |
|---|---|
Loading init.lua and its modules | 2 s total |
| One task step (until it finishes or waits) | 25 ms |
Live tasks (spawn refuses beyond this) | 1024 |
wait | 0 to 10,000,000 s |
| Chat and broadcast messages | 160 characters |
give amount | 1 to 10,000, and the item's maximum stack (300 for ammunition) per record |
| Command and event names | 1 to 32 of a-z 0-9 _ |
| Scenario folder name | 1 to 64 of A-Z a-z 0-9 _ - |
| Store key | 1 to 200 bytes |
| Store value / all store data | 256 KB / 4 MB of JSON |
| Store table nesting | 32 levels |
loot.json add count | 1 to 20 |
Found a mistake or missing detail? Tell us in the Discord.
rtv-mp