> For the complete documentation index, see [llms.txt](https://wiki.tiltedphoques.com/tilted-online/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wiki.tiltedphoques.com/tilted-online/guides/scripting/gameserver.md).

# GameServer

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

```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:

```lua
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:

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

***

<table data-full-width="true"><thead><tr><th>Name</th><th>Description</th><th>Usage</th></tr></thead><tbody><tr><td>get()</td><td>Get the GameServer instance</td><td>GameServer:get()</td></tr><tr><td>Kill()</td><td>Shut down server</td><td>GameServer:get():Kill()</td></tr><tr><td>Kick(int)</td><td>Kick the player with the given Connection Id</td><td>GameServer:get():Kick(player:GetConnectionId())</td></tr><tr><td>GetTick()</td><td>Get the server's current tick value</td><td>GameServer:get():GetTick()</td></tr><tr><td>SendChatMessage(int, string)</td><td>Send a message to the given Connection Id</td><td>GameServer:get():SendMessage(player:getConnectionId(), "This is a message")</td></tr><tr><td>SendGlobalChatMessage(string)</td><td>Send a message to all connected players</td><td>GameServer:get():SendGlobalChatMessage("This is a global message")</td></tr><tr><td>SetTime(int hours, int minutes, float timeScale)</td><td>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</td><td>GameServer:get():SetTime(11, 15, 20)<br>-- Set time to 11:15 AM at a time scale of 20</td></tr></tbody></table>

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

## Example

```lua
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)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://wiki.tiltedphoques.com/tilted-online/guides/scripting/gameserver.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
