GamesLauncherNewsDocsCommunityMy accountMy serversDownload Launcher
DocsLua scripting

Lua examples

Complete resources you can copy - welcome messages, an economy, admin commands and chat.

#Welcome messages

resources/welcome/resource.lua:

lua
name 'welcome'
version '1.0.0'
server_script 'server.lua'

resources/welcome/server.lua:

lua
local text = GetConvar('welcome_text', 'Welcome!')

AddEventHandler('playerJoining', function(id, name)
  SendMessage(id, text .. ' ' .. name)
  SendMessage(-1, name .. ' has arrived in the realm.')
end)

AddEventHandler('playerDropped', function(id, name)
  SendMessage(-1, name .. ' has left the realm.')
end)

#Economy saved in a JSON file

resources/economy/server.lua:

lua
local RES   = GetCurrentResourceName()
local FILE  = 'data/gold.json'
local START = GetConvarInt('start_gold', 100)
local gold  = json.decode(LoadResourceFile(RES, FILE) or '{}') or {}

local function key(id) return GetPlayerName(id) end   -- use a real account id in your game
local function save() SaveResourceFile(RES, FILE, json.encode(gold)) end

AddEventHandler('playerJoining', function(id)
  gold[key(id)] = gold[key(id)] or START
  TriggerClientEvent('economy:gold', id, gold[key(id)])
end)

RegisterNetEvent('economy:pay', function(targetId, amount)
  local from = source
  if type(targetId) ~= 'number' or type(amount) ~= 'number' then return end
  amount = math.floor(amount)
  if amount <= 0 or (gold[key(from)] or 0) < amount then return end
  if not GetPlayerName(targetId) then return end
  gold[key(from)]     = gold[key(from)] - amount
  gold[key(targetId)] = (gold[key(targetId)] or 0) + amount
  TriggerClientEvent('economy:gold', from, gold[key(from)])
  TriggerClientEvent('economy:gold', targetId, gold[key(targetId)])
end)

CreateThread(function()            -- autosave every 5 minutes
  while true do Wait(300000) save() end
end)

AddEventHandler('onResourceStop', function(res)
  if res == RES then save() end
end)

#Admin command: kick

lua
RegisterCommand('kick', function(src, args)
  local id = tonumber(args[1])
  local reason = table.concat(args, ' ', 2)
  if not id then print('Usage: kick <id> <reason>') return end
  DropPlayer(id, reason ~= '' and reason or 'Kicked by admin')
end)

#Simple chat

The client sends chat:send with ["text"]:

lua
RegisterNetEvent('chat:send', function(msg)
  if type(msg) ~= 'string' then return end
  msg = msg:sub(1, 200)
  TriggerClientEvent('chat:message', -1, GetPlayerName(source), msg)
end)

#Restart with a countdown

lua
RegisterCommand('restartwarn', function()
  CreateThread(function()
    for m = 5, 1, -1 do
      SendMessage(-1, 'The server restarts in ' .. m .. ' min.')
      Wait(60000)
    end
    for _, id in ipairs(GetPlayers()) do DropPlayer(id, 'Server restarting') end
  end)
end)

Last updated 2026-12-06