Events
Client Events
| Name | Description |
|---|---|
| scriptInit | Called after the player has connected to the server, joined the default session for the first time and all scripts have been loaded. |
| sessionInit | Called when the player has joined a session. |
| sessionFail | Called when the player failed to host/join a session. |
| chatCommand | Called when a command is entered in the chat. |
| chatSubmit | Called when the player submits a message in the chat. |
| webUIReady | Called after the web page has been loaded successfully. |
| windowFocusChange | Called when the game is focused/unfocused. |
Server Events
| Name | Description |
|---|---|
| playerDamage | Called when a player damages another player, before the damage reaches the target. |
| playerDisconnect | Called when a player disconnects. |
Shared Events
| Name | Description |
|---|---|
| resourceStart | Called after a resource started. |
| resourceStop | Called before a resource stops. |
scriptInit
Called after the player has connected to the server, joined the default session for the first time and all scripts have been loaded.
scriptInit()
Example:
- Lua
- JavaScript
- Squirrel (deprecated)
Events.Subscribe("scriptInit", function()
Chat.AddMessage("Welcome to my server.")
end)
Events.Subscribe("scriptInit", () => {
Chat.AddMessage("Welcome to my server.");
});
Events.Subscribe("scriptInit", function() {
Chat.AddMessage("Welcome to my server.");
});
sessionInit
Called when the player has joined a session.
sessionInit()
sessionFail
Called when the player failed to host/join a session.
sessionFail(int attempt)
You can cancel this event, to stop the default retry behaviour.
chatCommand
Called when a command is entered in the chat.
chatCommand(string fullChatMessage)
This event will be only called if the default chat is activated in the server settings.
Example:
- Lua
- JavaScript
- Squirrel (deprecated)
Events.Subscribe("chatCommand", function(command)
if command == "/testcmd" then
Chat.AddMessage("Test Command!")
end
end)
Events.Subscribe("chatCommand", (command) => {
if (command === "/testcmd") {
Chat.AddMessage("Test Command!");
}
});
Events.Subscribe("chatCommand", function(command) {
if (command == "/testcmd") {
Chat.AddMessage("Test Command!");
}
});
chatSubmit
Called when the player submits a message in the chat.
chatSubmit(string chatMessage)
You can cancel this event, to stop the message being sent.
Example:
- Lua
- JavaScript
- Squirrel (deprecated)
Events.Subscribe("chatSubmit", function(message)
if string.find(message, "bitch") ~= nil then
Events.Cancel() -- don't send message with bad word
end
end)
Events.Subscribe("chatSubmit", (message) => {
if (message.includes("bitch")) {
Events.Cancel(); // don't send message with bad word
}
});
Events.Subscribe("chatSubmit", function(message) {
if (message.find("bitch") != null) {
Events.Cancel(); // don't send message with bad word
}
});
webUIReady
Called after the web page has been loaded successfully.
webUIReady(int webuiID)
It is called again every time the page loads anew: after WebUI.LoadURL and WebUI.Reload, when the page reloads itself or follows a link, and when the WebUI was restored after its browser crashed. Send the data the page needs from this event, not only once after creating the WebUI.
WebUI.GetEventSource returns the same ID as webuiID.
windowFocusChange
Called when the game is focused/unfocused.
windowFocusChange(bool isFocused)
playerDamage
Called when a player damages another player, before the damage reaches the target.
playerDamage(int targetID, int weapon, int weaponDamage, bool damageOverride, int pedComponent)
Events.GetSource returns the player who caused the damage,
targetID is the one being hit.
| Parameter | Description |
|---|---|
| targetID | Server ID of the player taking the damage. |
| weapon | The weapon used, see Weapons. |
| weaponDamage | Only carries a value while damageOverride is true, for example when a car rams a player. For an ordinary shot it is 0 and the target applies the damage the weapon is worth. |
| damageOverride | Whether weaponDamage holds an explicit value. |
| pedComponent | Body part that was hit. |
You can cancel this event. The target then takes no damage at all, and no death or health change follows from this hit.
This event requires the gameevents server setting. While that is off the event never fires, and a handler for it simply never runs.
Only damage from one player to another reaches this event. Damage to vehicles, objects or non-player peds does not.
Example:
- Lua
- JavaScript
- Squirrel (deprecated)
local protected = {}
Events.Subscribe("playerDamage", function(targetID, weapon, weaponDamage, damageOverride, pedComponent)
if protected[targetID] then
Events.Cancel()
return
end
Chat.BroadcastMessage(Player.GetName(Events.GetSource()) .. " hit " .. Player.GetName(targetID))
end)
const protectedPlayers = new Set();
Events.Subscribe("playerDamage", (targetID, weapon, weaponDamage, damageOverride, pedComponent) => {
if (protectedPlayers.has(targetID)) {
Events.Cancel();
return;
}
Chat.BroadcastMessage(Player.GetName(Events.GetSource()) + " hit " + Player.GetName(targetID));
});
protected <- {};
Events.Subscribe("playerDamage", function(targetID, weapon, weaponDamage, damageOverride, pedComponent) {
if (targetID in protected) {
Events.Cancel();
return;
}
Chat.BroadcastMessage(Player.GetName(Events.GetSource()) + " hit " + Player.GetName(targetID));
});
playerDisconnect
Called when a player disconnects.
playerDisconnect(int serverID, string playerName, int reason)
Reason IDs:
0: timeout
1: quit
2: kick
Example:
- Lua
- JavaScript
- Squirrel (deprecated)
Events.Subscribe("playerDisconnect", function(id, name, reason)
Chat.BroadcastMessage("{0000FF}" .. name .. " {FFFFFF}has left the server.")
end)
Events.Subscribe("playerDisconnect", (id, name, reason) => {
Chat.BroadcastMessage("{0000FF}" + name + " {FFFFFF}has left the server.");
});
Events.Subscribe("playerDisconnect", function(id, name, reason) {
Chat.BroadcastMessage("{0000FF}" + name + " {FFFFFF}has left the server.");
});
resourceStart
Called after a resource started.
resourceStart(string resourceName)
resourceStop
Called before a resource stops.
resourceStop(string resourceName)
If this event is canceled by a server-side handler, the resource is not stopped.