Events
These event APIs allow you to send and receive data between different scripts or resources. Events only work cross-resources within the same context. For example:
- A client script fires an event. Only other client scripts can listen for this event.
- A server script fires an event. Only other server scripts can listen for this event.
- A shared script fires an event. Client, server, or shared scripts can listen for this event.
Important Note
You may only subscribe once per event per file. If you subscribe multiple times, only the first subscription will work properly. For example:
Event.Add("OnRender", function() -- Works!
Render.DrawLine(vec2(200, 200), vec2(200, 1000), 100, vec4(0.0, 1, 0.0, 1.0))
end)
Event.Add("OnRender", function() -- Does not work
Render.DrawLine(vec2(200, 200), vec2(200, 1000), 1000, vec4(1, 1, 0.0, 1.0))
end)
In the above example, only the first Render event will work properly and draw a line to the screen. The second render event will not work - a line will not be drawn to the screen because an event with the name Render has already been added.
Global Methods
Event.Add(event: string, handler: function)
Adds an event handler for the specified event name.
Event.Fire(event: string, data: any)
Triggers a corresponding event in any resource that has a handler Event.Add with the same event name.
Example usage:
-- resources/test/client/main.lua
Cmd.Add("test", function()
Event.Fire("testEvent", { data = 2 })
end)
A corresponding client script can listen for the event:
-- resources/other/client/hello.lua
Event.Add("test", function(args)
print("Received: " .. tostring(args.data))
end)
Event.Remove(event: string)
Removes an event handler for the specified event name in a script.
Events
List of all events on both the client and server.
Resource Events
OnResourceStart(resource: string)
Called when resource starts when a client joins the server. The first argument is the name of the resource that started. If you want to enable abilities for the local player, you should do it here (only on the client).
Parameters:
resource: string- The name of the resource that started
Example:
Event.Add("OnResourceStart", function(resource)
if resource == Resource.Name then
Local.UnlockAbility(Ability.GrapplingHook)
Local.UnlockAbility(Ability.Parachute)
Local.UnlockAbility(Ability.Wingsuit)
Local.UnlockAbility(Ability.ExitVehicle)
end
end)
OnResourceStop(resource: string)
Called when resource stops. The first argument is the name of the resource that stopped.
Parameters:
resource: string- The name of the resource that stopped
Example:
Event.Add("OnResourceStop", function(resource)
print("Resource stopped: " .. resource)
end)
Player Events
OnPlayerDamage(player: NetPlayer, damager: NetObject, loss: number, hitbone: number, weaponHash: number, hitposition: vec3)
This event fires when a player is damaged. Use Event.Cancel() (only on the server) to prevent the player from taking damage.
Parameters:
player: NetPlayer- The player that was damageddamager: NetObject- The object that caused the damageloss: number- The amount of damage dealthitbone: number- The bone index that was hitweaponHash: number- The weapon hash usedhitposition: vec3- The world position where the hit occurred
Example:
Event.Add("OnPlayerDamage", function(player, damager, loss, hitbone, weaponHash, hitposition)
print("Player " .. player:GetNick() .. " took " .. loss .. " damage")
-- Cancel damage on server
if Server then
Event.Cancel()
end
end)
OnPlayerKilled(player: NetPlayer, damager: NetObject, loss: number, hitbone: number, weaponHash: number, hitposition: vec3)
This event fires when a player is killed. Parameters match OnPlayerDamage event for consistency.
Parameters:
player: NetPlayer- The player that was killeddamager: NetObject- The object that caused the deathloss: number- The amount of damage dealthitbone: number- The bone index that was hitweaponHash: number- The weapon hash usedhitposition: vec3- The world position where the hit occurred
Example:
Event.Add("OnPlayerKilled", function(player, damager, loss, hitbone, weaponHash, hitposition)
print("Player " .. player:GetNick() .. " was killed")
end)
Vehicle Events
OnVehicleDestroy(vehicle: NetVehicle, damager: NetObject, loss: number, hitbone: number, weaponHash: number, hitposition: vec3)
This event fires when a vehicle is destroyed.
Parameters:
vehicle: NetVehicle- The vehicle that was destroyeddamager: NetObject- The object that caused the destructionloss: number- The amount of damage dealthitbone: number- The bone index that was hitweaponHash: number- The weapon hash usedhitposition: vec3- The world position where the hit occurred
Example:
Event.Add("OnVehicleDestroy", function(vehicle, damager, loss, hitbone, weaponHash, hitposition)
print("Vehicle " .. vehicle:GetId() .. " was destroyed")
end)
OnVehicleDamage(vehicle: NetVehicle, damager: NetObject, loss: number, hitbone: number, weaponHash: number, hitposition: vec3)
This event fires when a vehicle is damaged.
Parameters:
vehicle: NetVehicle- The vehicle that was damageddamager: NetObject- The object that caused the damageloss: number- The amount of damage dealthitbone: number- The bone index that was hitweaponHash: number- The weapon hash usedhitposition: vec3- The world position where the hit occurred
Example:
Event.Add("OnVehicleDamage", function(vehicle, damager, loss, hitbone, weaponHash, hitposition)
print("Vehicle " .. vehicle:GetId() .. " took " .. loss .. " damage")
end)
Player Action Events
OnPlayerTeleport(player: NetPlayer)
This event fires when a player teleports (via NetPlayer:Teleport).
Parameters:
player: NetPlayer- The player that teleported
Example:
Event.Add("OnPlayerTeleport", function(player)
print("Player " .. player:GetNick() .. " teleported")
end)
Communication Events
OnChat(sender: NetPlayer, message: string)
This event fires when a player sends a chat message. On the server, use Event.Cancel() to block the message.
Parameters:
sender: NetPlayer- The player who sent the messagemessage: string- The chat message content
Example:
Event.Add("OnChat", function(sender, message)
print(sender:GetNick() .. ": " .. message)
-- Block message on server
if Server then
Event.Cancel()
end
end)