> For the complete documentation index, see [llms.txt](https://docs.otland.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.otland.net/tfs-documentation/luascript-interface/globalevents.md).

# GlobalEvents

* [**onStartup**](#onstartup)**()**
* [**onShutdown**](#onshutdown)**()**
* [**onRecord**](#onrecord)**(current, old)**
* [**onTime**](#ontime)**(interval)**
* [**onThink**](#onthink)**(interval)**

### onStartUp()

**onStartup has no parameters, requires no return, and&#x20;*****only runs once*****&#x20;during the start of the server.**

**Example** *startup.lua*

```lua
function onStartup()
    print("There are " ..#Game.getHouses().. " houses registered to this map")
end
```

**You can register in data/globalevents/globalevents.xml by using type="*****startup*****" like so:**

```xml
<globalevent type="startup" name="ServerStartup" script="startup.lua" />
```

### onShutdown()

**onShutdown has no parameters, requires no return, and&#x20;*****only runs once*****&#x20;during the shutdown of the server**

**Example** *shutdown.lua*

```lua
function onShutdown()
    for index, player in pairs(Game.getPlayers()) do
        player:save()
    end
end
```

**You can register in data/globalevents/globalevents.xml by using type="*****shutdown*****" like so:**

```xml
<globalevent type="shutdown" name="ServerShutdown" script="shutdown.lua" />
```

### onRecord(current, old)

**onRecord has two parameters,&#x20;*****requires return true*****, and runs each time a new record is made for most players online**

> * **current** -- number value of new record, for most players online
> * **old** -- number value of old record, for most players online

**Example** *record.lua*

```lua
function onRecord(current, old)
    addEvent(Game.broadcastMessage, 150, "New record: " .. current .. " players are logged in.", MESSAGE_STATUS_DEFAULT)
    return true
end
```

**You can register in data/globalevents/globalevents.xml by using type="*****record*****" like so:**

```xml
<globalevent type="record" name="PlayerRecord" script="record.lua" />
```

### onTime()

**onTime has one parameter which represents the&#x20;*****time*****&#x20;it executes, requires return true, and runs each each day at same time**

**Example** *ontime.lua*

```lua
function onTime(interval)
    Game.startRaid("Dragons")
    return true
end
```

**You can register in data/globalevents/globalevents.xml by giving it a unique name and setting the time using time="*****time*****" like so:**

```xml
<globalevent name="StartDragonRaid" time="09:55:00" script="ontime.lua" />
```

### onThink()

**onThink has one parameter which represents how often it executes in&#x20;*****milliseconds*****, requires return true, and runs as often as specified in&#x20;*****globalevents.xml***

**Example** *onthink.lua*

```lua
function onThink(interval)
    for index, player in pairs(Game.getPlayers()) do
        player:save()
    end
    return true
end
```

**You can register in data/globalevents/globalevents.xml by giving it a unique name and setting how often it will execute using interval="1000"&#x20;*****time is in milliseconds***

```xml
<globalevent name="SaveOnThink" interval="10000" script="onthink.lua" />
```

{% hint style="warning" %}
The above will execute every **ten** seconds **10** \* 1000 = ***10000***
{% endhint %}


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.otland.net/tfs-documentation/luascript-interface/globalevents.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
