Event Handlers

Name
Description
Usage

onCharacterMove(int)

Called any time a character moves

addEventHandler("onCharacterMove", function(entityId)

onCharacterSpawn(int)

Called when any character spawns in

addEventHandler("onCharacterSpawn", function(entityId)

onCharacterDestroy(int)

Called when any character is destroyed

addEventHandler("onCharacterDestroy", function(entityId)

onPlayerJoin(int)

Called when a player joins the server

addEventHandler("onPlayerJoin", function(connectionId)

onChatMessage(int, string)

Called when a player sends a chat message

addEventHandler("onChatMessage", function(entityId)

onPlayerQuit(int, string)

Called when a player leaves the server

addEventHandler("onPlayerLeave", function(connectionId, reason)

onUpdate(float)

Called every server tick

addEventHandler("onUpdate", function(delta)

Example

local playerMgr = PlayerManager:get()
local gameServer = GameServer:get()

function getPlayer(entityId)
  local players = playerMgr:GetAllPlayers()
  for k, player in pairs(players) do
    if player:GetCharacter() == entityId then
      return player
    end
  end
  return nil
end

addEventHandler("onUpdate", function(delta)
  print("This is called every tick")
end)

addEventHandler("onPlayerJoin", function (connId)
  local player = getPlayer(connId)
  if not player then
    return
  end
  gameServer:SendGlobalChatMessage(player:GetUsername() .. " joined the server")
end)

addEventHandler("onPlayerQuit", function(connId, reason)
  local player = getPlayer(connId)
  if not player then
    return
  end
  gameServer:SendGlobalChatMessage(player:GetUsername() .. " left the server")
end)

addEventHandler("onChatMessage", function(entityId, message)
  local player = getPlayer(entityId)
  if not player then
    return
  end
  print(player:GetUsername() .. ": " .. message)
end)

addEventHandler("onCharacterSpawn", function(entityId)
  local player = getPlayer(entityId)
  if player then
    return
  end
  gameServer:SendGlobalChatMessage("Non-player with ID " .. entityId .. " spawned")
end)

addEventHandler("onCharacterDestroy", function(entityId)
  local player = getPlayer(entityId)
  if player then
    return
  end
  gameServer:SendGlobalChatMessage("Non-player with ID " .. entityId .. " destroyed")
end)

addEventHandler("onSetTime", function(hours, minutes, timeScale)
  gameServer:SendGlobalChatMessage("Time set to " .. hours .. ":" .. minutes .. " at time scale of " .. timeScale)
end)

Last updated