<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.multitheftauto.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=PieT</id>
	<title>Multi Theft Auto: Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.multitheftauto.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=PieT"/>
	<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/wiki/Special:Contributions/PieT"/>
	<updated>2026-05-12T01:31:29Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=TriggerServerEvent&amp;diff=53262</id>
		<title>TriggerServerEvent</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=TriggerServerEvent&amp;diff=53262"/>
		<updated>2018-01-02T19:23:41Z</updated>

		<summary type="html">&lt;p&gt;PieT: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;!--{{Needs_Checking|Something needs to be said about the steps required to help keep an event inside a resource. i.e. Setting 'theElement' to resourceRoot here, and setting the matching event handler's 'attachedTo' also to resourceRoot.}}--&amp;gt;__NOTOC__&lt;br /&gt;
{{Client function}}&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Almost any data types can be passed as expected, including [[element]]s and complex nested [[table]]s. Non-element MTA data types like xmlNodes or resource pointers will not be able to be passed as they do not necessarily have a valid representation on the client. '''Elements of the Vector or Matrix classes cannot be passed!'''&lt;br /&gt;
&lt;br /&gt;
Events are sent reliably, so the server will receive them, but there may be (but shouldn't be) a significant delay before they are received. You should take this into account when using them.&lt;br /&gt;
&lt;br /&gt;
Keep in mind the bandwidth issues when using events - don't pass a large list of arguments unless you really need to. It is marginally more efficient to pass one large event than two smaller ones.&lt;br /&gt;
{{Warning|You should use the global variable [[Predefined_variables_list|''client'']] serverside instead of passing the localPlayer by parameter or source. Otherwise event faking (passing another player instead of the localPlayer) would be possible. More information at [[addEventHandler]]}}&lt;br /&gt;
==Syntax== &lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
bool triggerServerEvent ( string event, element theElement, [arguments...] )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
===Required Arguments=== &lt;br /&gt;
*'''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]].&lt;br /&gt;
*'''theElement:''' The element that is the [[Event system#Event handlers|source]] of the event.&lt;br /&gt;
{{Note|To save server CPU, you should avoid setting '''theElement''' to the [[root element]] where possible. Using [[GetThisResource|resourceRoot]] is usually sufficient if the event is handled by the same resource on the server.}}&lt;br /&gt;
===Optional Arguments=== &lt;br /&gt;
*'''arguments...:''' A list of arguments to trigger with the event. You can pass any lua data type (except functions). You can also pass [[element]]s.&lt;br /&gt;
&lt;br /&gt;
===Returns===&lt;br /&gt;
Returns ''true'' if the event trigger has been sent, ''false'' if invalid arguments were specified or a client side element was a parameter.&lt;br /&gt;
&lt;br /&gt;
==Example== &lt;br /&gt;
This example shows how you can pass a simple &amp;quot;Hello World&amp;quot; message from the client to the server using an event.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;section name=&amp;quot;Server&amp;quot; class=&amp;quot;server&amp;quot; show=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function greetingHandler ( message )&lt;br /&gt;
	-- the predefined variable 'client' points to the player who triggered the event and should be used due to security issues   &lt;br /&gt;
	outputChatBox ( &amp;quot;The client says: &amp;quot; .. message, client )&lt;br /&gt;
end&lt;br /&gt;
addEvent( &amp;quot;onGreeting&amp;quot;, true )&lt;br /&gt;
addEventHandler( &amp;quot;onGreeting&amp;quot;, resourceRoot, greetingHandler ) -- Bound to this resource only, saves on CPU usage.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/section&amp;gt;&lt;br /&gt;
&amp;lt;section name=&amp;quot;Client&amp;quot; class=&amp;quot;client&amp;quot; show=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function greetingCommand ( commandName )&lt;br /&gt;
    triggerServerEvent ( &amp;quot;onGreeting&amp;quot;, resourceRoot, &amp;quot;Hello World!&amp;quot; )&lt;br /&gt;
    -- Source can be this resource as it saves on CPU and prevents event name conflicts with other resources but you can't use resourceRoot if another resource will handle the event.&lt;br /&gt;
end&lt;br /&gt;
addCommandHandler ( &amp;quot;greet&amp;quot;, greetingCommand )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/section&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When the command &amp;quot;greet&amp;quot; is executed (by typing it in the server console or the player's console), the clients ''greetingCommand'' function is called. This triggers the server-side event ''onGreeting'' with the string ''&amp;quot;Hello World!&amp;quot;''. This event is then handled by the ''greetingHandler'' function server-side which then displays the message.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
{{Client_event_functions}}&lt;br /&gt;
[[ru:triggerServerEvent]]&lt;/div&gt;</summary>
		<author><name>PieT</name></author>
	</entry>
</feed>