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)
Name | Description | Usage |
---|---|---|
get() | Get the GameServer instance | GameServer:get() |
Kill() | Shut down server | GameServer:get():Kill() |
Kick(int) | Kick the player with the given Connection Id | GameServer:get():Kick(player:GetConnectionId()) |
GetTick() | Get the server's current tick value | GameServer:get():GetTick() |
SendChatMessage(int, string) | Send a message to the given Connection Id | GameServer:get():SendMessage(player:getConnectionId(), "This is a message") |
SendGlobalChatMessage(string) | Send a message to all connected players | GameServer: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 scale | GameServer: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 %}
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)