Docs/Guides

Recipes

Copy-paste Lua examples for rtv-mp scenarios: per-player data, welcome gifts after spawning, raid events that clean up after themselves, and native event hooks.

API version 1Lua 5.1Updated 2026-11-07

Per-player data for one session. Key tables by identity, not by handle or ID. Use store instead when it should survive reloads and restarts.

local kills = {}
on("npc_killed", function(npc, killer)
  if killer and killer.identity then
    kills[killer.identity] = (kills[killer.identity] or 0) + 1
  end
end)
command("kills", function(player)
  player:tell("NPC kills this session: " .. (kills[player.identity] or 0))
end)

The data lives in Lua memory, so a restart or /scenario reload resets it.

Welcome gift after the starting kit arrives.

on("player_spawn", function(player, reason)
  if reason ~= "join" then
    return
  end
  wait(5)
  local ok, err = player:give("Bandage", 2)
  if not ok then
    print("gift failed for " .. player.name .. ": " .. err)
  end
end)

An event that tracks the NPCs it spawned and removes survivors.

local spawned = {} -- event -> list of NPCs

register_event("raid", {
  maps = {"Village"},
  chance = 15,
  duration = 900,
  cleanup = function(area, event)
    for _, npc in ipairs(spawned[event] or {}) do
      if npc.alive then
        npc:kill()
      end
    end
    spawned[event] = nil
  end,
}, function(area, event)
  spawned[event] = {}
  for i = 1, 4 do
    local position = area:random_position()
    local npc = position and area:spawn_npc(position, "military")
    if npc then
      table.insert(spawned[event], npc)
    end
    wait(2)
  end
  area:tell("A military raid has entered the area.")
end)

Reacting to native events.

on("event_started", function(area, name, scripted)
  if not scripted and name == "airdrop" then
    area:tell("A supply plane is inbound.")
  end
end)

For examples that place or remove objects, see Object placement.

Night-time announcement using the console.

command("night", function(player)
  if not player.admin then
    return player:tell("Admins only.")
  end
  local lines, err = server.console("/time 22:00")
  player:tell(lines and lines[1] or err)
end)

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