Docs/API

Events

Define custom events with register_event in rtv-mp scenarios: options, how chance rolling works, the event lifecycle, how events end, and event handles.

API version 1Lua 5.1Updated 2026-11-07

A scenario event is a named function that the server starts in an area by chance on a timer, or on demand with area:start_event(name) or /scenario start. Each event runs at most once per area instance at a time.

Scenario events have no visuals of their own on clients: an event is whatever its function does with the rest of the API (NPCs, messages, native events, items). They run alongside native dynamic events and never replace them.

register_event(name, {
  maps = {"Village", "Highway"},   -- public maps it may roll in (default: every public map)
  chance = 25,                    -- percent per roll, 0-100 (default 100)
  interval = 900,                 -- seconds between rolls per area, 10-604800 (default 900)
  min_players = 1,                -- players needed in the area for a roll to count, 0-64 (default 1)
  duration = 600,                 -- seconds before it ends by itself, 0-604800 (default 0 = see below)
  cleanup = function(area, event) end,  -- runs once when it ends (optional)
}, function(area, event)
  -- the event's main task
end)

The options table is optional: register_event("drill", function(area, event) ... end) uses every default. The name follows the same rules as command names and cannot be a native event key (airdrop, btr_patrol, ...). Each name can be registered once.

Rolling#

  • Every loaded public map in maps gets its own roll timer for each event.
  • The first roll is one interval after the area is first seen (when it loads, or when the scenario loads or reloads). Later rolls follow every interval.
  • At each roll the event starts if it is not already running there, the area has at least min_players players, and a random number from 0 to 99 is below chance. chance = 0 never rolls but can still be started by hand.
  • Scenario events never start in private hideouts.

Lifecycle#

  1. event_started(area, name, true) hooks fire.
  2. The main function runs as a task with (area, event) and may wait.
  3. The event ends (see below).
  4. Its main task is stopped if it is still running. A function that ends its own event stops at its next wait.
  5. event_ended(area, name, reason, true) hooks fire, then cleanup(area, event) runs as its own task.

How a scenario event ends#

ReasonWhen
"finished"No duration, and the main function returned.
"error"No duration, and the main function failed.
"duration"duration seconds have passed since it started. With a duration, the event keeps running after its main function returns.
"stopped"event:stop(), area:stop_event(name) or /scenario stop.
"area_unloaded"Its area was unloaded.

A reload drops running events without ending them (see reloading).

Event handles#

event:stop()#

Ends the event with reason "stopped". Does nothing if it already ended.

Field / methodDescription
event.nameThe event name.
event.areaThe area handle it runs in.
event.runningfalse once it has ended.
event.started_atStart time (server.time() seconds).
event.ends_atWhen duration ends it, or nil without a duration.

Event handles compare with ==, so they work as table keys and owners (see Recipes). Tasks the event function starts with spawn, after or every belong to the scenario, not the event: cancel them in cleanup if they should stop with it.

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