triggerServerEvent
Manual Review Required
Please finish this page using the corresponding Old Wiki article. Go to Contribution guidelines for more information.
This function triggers an event previously registered on the server. This is the primary means of passing information between the client and the server. Servers have a similar triggerClientEvent function that can do the reverse. You can treat this function as if it was an asynchronous function call, using triggerClientEvent to pass back any returned information if necessary.
It is marginally more efficient to pass one large event than two smaller ones.
You should use the global variable client server-side instead of passing the localPlayer by parameter or source. Otherwise event faking (passing another player instead of the localPlayer ) would be possible. For more information see: Script security article.
To save server CPU, you should avoid setting theElement to the root element where possible - it should be used as a last resort (rather questionable thing to do, limited to very specific tasks, if any). Using localPlayer is preferred and highly advisable. resourceRoot can also be used as alternative choice, if addEventHandler is bound to root element, or to resourceRoot when there is need to restrict event to single certain resource (although cheater could still trigger it from different resource, by using getResourceRootElement and passing respective resource root element)
Syntax
bool triggerServerEvent ( string event, element theElement, unknown arguments... )Required Arguments
- event: The name of the event to trigger server-side. You should register this event with addEvent and add at least one event handler using addEventHandler .
- theElement: The element that is the source of the event.
- arguments...: A list of arguments to trigger with the event. You can pass any lua data type (except functions). You can also pass elements .
Returns
- bool: value
Returns true if the event trigger has been sent, false if invalid arguments were specified or a client side element was a parameter.
Code Examples
function sendMessageToServer() local messageToSend = "Hello, world!" -- this string would be passed to server
triggerServerEvent("onServerSendMessage", localPlayer, messageToSend) -- refer to the note on wiki page (under theElement), for understanding which element you should use as 2nd argumentendaddCommandHandler("message", sendMessageToServer)