Skip to content

Latest commit

 

History

History
58 lines (43 loc) · 2.72 KB

gameserver.md

File metadata and controls

58 lines (43 loc) · 2.72 KB

GameServer

GameServer is a singleton instance. To fetch use, use the following lua:

local gameServer = GameServer:get()

Interact with the GameServer After obtaining the game server instance, you can call methods from the GameServer class using the colon operator (:) . For example:

gameServer:Kill(playerId)
gameServer:Kick(playerId)
local serverTick = gameServer:GetTick()

Send a chat message To send a chat message from the server to a specific player, use the SendChatMessage method:

local connectionId = 1 -- Replace this with the target player's connection ID
local message = "Hello from the server!"
gameServer:SendChatMessage(connectionId, message)

NameDescriptionUsage
get()Get the GameServer instanceGameServer:get()
Kill()Shut down serverGameServer:get():Kill()
Kick(int)Kick the player with the given Connection IdGameServer:get():Kick(player:GetConnectionId())
GetTick()Get the server's current tick valueGameServer:get():GetTick()
SendChatMessage(int, string)Send a message to the given Connection IdGameServer:get():SendMessage(player:getConnectionId(), "This is a message")
SendGlobalChatMessage(string)Send a message to all connected playersGameServer:get():SendGlobalChatMessage("This is a global message")
SetTime(int hours, int minutes, float timeScale)Set the world's time to the specified hour and minutes at the given time scale (default time scale is whatever the current world time scaleGameServer:get():SetTime(11, 15, 20)
-- Set time to 11:15 AM at a time scale of 20

{% hint style="info" %} NOTE: SendChatMessage and SendGlobalChatMessage are automatically sanitized to remove any HTML tags {% endhint %}

Example

local gameServer = GameServer:get()

addEventHandler("onPlayerQuit", function(entityId)
  gameServer:SendGlobalChatMessage("Someone left, server sad, server close :(")
  gameServer:Kill()
end)

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

addEventHandler("onChatMessage", function(entityId, message)
  local player = getPlayer(entityId)
  if not player then
    return
  end
  
  if(string.find(message, "a") then
    gameServer:SendMessage(player:GetConnectionId(), "Your message contains an a, you're kicked")
    gameServer:Kick(player:GetConnectionId())
  end
end)