Skip to content

Latest commit

 

History

History
68 lines (57 loc) · 2.73 KB

event-handlers.md

File metadata and controls

68 lines (57 loc) · 2.73 KB

Event Handlers

NameDescriptionUsage
onCharacterMove(int)Called any time a character movesaddEventHandler("onCharacterMove", function(entityId)
onCharacterSpawn(int)Called when any character spawns in addEventHandler("onCharacterSpawn", function(entityId)
onCharacterDestroy(int)Called when any character is destroyedaddEventHandler("onCharacterDestroy", function(entityId)
onPlayerJoin(int)Called when a player joins the serveraddEventHandler("onPlayerJoin", function(connectionId)
onChatMessage(int, string)Called when a player sends a chat messageaddEventHandler("onChatMessage", function(entityId)
onPlayerQuit(int, string)Called when a player leaves the serveraddEventHandler("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)