- Documentation
- Client functions
- triggerServerEvent
Function
triggerServerEvent
Sends an event from the player to the server, for example a button click or a menu choice.
Syntax
bool triggerServerEvent(string name [, element source = localPlayer, ...])Arguments
namestring- Event name. The server must register it with
addEvent(name, true). sourceelement, optional = localPlayer- The element the server will see as
source. ...any, optional- Arguments for the handler on the server.
Returns
true if the event was sent.
Example
The player presses F2 and the server checks whether they can afford a car repair.
bindKey("F2", "down", function()
triggerServerEvent("repairCar", resourceRoot)
end)
addEvent("repairCar", true)
addEventHandler("repairCar", resourceRoot, function()
-- client is the player who sent the event
local car = getPedOccupiedVehicle(client)
if not car then return end
if getPlayerMoney(client) < 500 then
outputChatBox("A repair costs $500.", client, 255, 80, 80)
return
end
takePlayerMoney(client, 500)
fixVehicle(car)
end)
Notes
Do not trust data from the player. On the server use the client variable, not arguments sent by the player, to know who sent the event. Always check prices, rewards and permissions on the server.
Good to know. An event not registered with true is rejected, and the server triggers onPlayerTriggerInvalidEvent.