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

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

The question should be specific, self-contained, and written in natural language.
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.
