<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
	<id>https://rage.grandroleplay.ru/index.php?action=history&amp;feed=atom&amp;title=Getting_Started_with_Entity_variables</id>
	<title>Getting Started with Entity variables - История изменений</title>
	<link rel="self" type="application/atom+xml" href="https://rage.grandroleplay.ru/index.php?action=history&amp;feed=atom&amp;title=Getting_Started_with_Entity_variables"/>
	<link rel="alternate" type="text/html" href="https://rage.grandroleplay.ru/index.php?title=Getting_Started_with_Entity_variables&amp;action=history"/>
	<updated>2026-07-25T00:50:23Z</updated>
	<subtitle>История изменений этой страницы в вики</subtitle>
	<generator>MediaWiki 1.43.9</generator>
	<entry>
		<id>https://rage.grandroleplay.ru/index.php?title=Getting_Started_with_Entity_variables&amp;diff=10149&amp;oldid=prev</id>
		<title>imported&gt;SugarD-x: Fixed a typographical error.</title>
		<link rel="alternate" type="text/html" href="https://rage.grandroleplay.ru/index.php?title=Getting_Started_with_Entity_variables&amp;diff=10149&amp;oldid=prev"/>
		<updated>2020-11-19T22:25:37Z</updated>

		<summary type="html">&lt;p&gt;Fixed a typographical error.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
In this page you&amp;#039;ll learn about how to use &amp;#039;&amp;#039;&amp;#039;Entity&amp;#039;&amp;#039;&amp;#039; variables. This is a list of functions that are related to the entity variables:&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Functions&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
**[[Entity::getVariable|getVariable]]&lt;br /&gt;
**[[Entity::setVariable|setVariable]]&lt;br /&gt;
**[[Events::addDataHandler|addDataHandler]]&lt;br /&gt;
&lt;br /&gt;
== What is entity variables? ==&lt;br /&gt;
&lt;br /&gt;
Entity variables are custom assigned data to a certain entity to identify it either locally or globally to all other clients on the server. You have the flexibility to choose whether to keep it private or public.&lt;br /&gt;
&lt;br /&gt;
== Synced entity variables ==&lt;br /&gt;
Synced entity variables are custom data that are assigned to the entity globally so other clients can read these data. Synced entity variables are only writable server-side, which means no one can overwrite data from client-side. &lt;br /&gt;
Let&amp;#039;s say you need to keep your scoreboard updated every 3 seconds and it displays every player&amp;#039;s cash, ping, job, and etc. To keep things up to date you&amp;#039;ll definitely need to use synced entity variables to display updated values since for example a player made an transaction then his cash is updated server-side, then the scoreboard should automatically update it in the next refresh call by getting each player&amp;#039;s synced variable. There are many applications for entity synced variables and you can explore [https://rage.mp/files/category/3-scripts/ resources] to get to know more about it.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
We&amp;#039;ll do a short example about money HUD and how it works with entity synced variables.&lt;br /&gt;
&lt;br /&gt;
Firstly we&amp;#039;ll setup our server-side simple money API&lt;br /&gt;
&lt;br /&gt;
In server-side we did a small manager that accepts withdrawal/deposit money and updates the synced entity variable of the player that requested the operation.&lt;br /&gt;
{{ServersideCode|&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
mp.events.add({&lt;br /&gt;
    &amp;#039;playerReady&amp;#039;: (player) =&amp;gt; {&lt;br /&gt;
        if (player) {&lt;br /&gt;
            /*&lt;br /&gt;
             * Setting our start cash value by 0&lt;br /&gt;
             * You can sync it via Database if you got one.&lt;br /&gt;
             * player.data.cash = 0 can work too &lt;br /&gt;
             */&lt;br /&gt;
            player.setVariable(&amp;#039;cash&amp;#039;, 0);&lt;br /&gt;
            // player.data.cash = 0&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    &amp;#039;withdraw&amp;#039;: (player, amount) =&amp;gt; {&lt;br /&gt;
        let currentMoney = player.getVariable(&amp;#039;cash&amp;#039;); // Getting base cash from entity.&lt;br /&gt;
        if (currentMoney &amp;amp;&amp;amp; currentMoney &amp;gt;= 0 &amp;amp;&amp;amp; amount &amp;gt; 0) {&lt;br /&gt;
            currentMoney += amount;&lt;br /&gt;
            player.setVariable(&amp;#039;cash&amp;#039;, currentMoney); // Setting base cash of entity to new value.&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    &amp;#039;deposit&amp;#039;: (player, amount) =&amp;gt; {&lt;br /&gt;
        let currentMoney = player.getVariable(&amp;#039;cash&amp;#039;); // Getting base cash from entity.&lt;br /&gt;
        if (currentMoney &amp;amp;&amp;amp; currentMoney &amp;gt;= 0 &amp;amp;&amp;amp; currentMoney &amp;gt;= amount) {&lt;br /&gt;
            currentMoney -= amount;&lt;br /&gt;
            player.setVariable(&amp;#039;cash&amp;#039;, currentMoney); // Setting base cash of entity to new value.&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
Now on client-side we should do a small money HUD that&amp;#039;ll display the cash and update once an operation has been made.&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;NOTE&amp;#039;&amp;#039;&amp;#039;: [[Events::addDataHandler|addDataHandler]] triggers on all clients when one entity updates its synced variable, so make sure to specify your scope if its per player operation.&lt;br /&gt;
{{ClientsideCode|&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
const localPlayer = mp.players.local;&lt;br /&gt;
let currentMoney;&lt;br /&gt;
&lt;br /&gt;
mp.events.add(&amp;#039;render&amp;#039;, () =&amp;gt; {&lt;br /&gt;
    if (currentMoney) { // Draws money HUD only if currentMoney is initalized&lt;br /&gt;
        mp.game.graphics.drawText(&amp;#039;$&amp;#039; + currentMoney, [0.85, 0.1], { &lt;br /&gt;
            font: 7, &lt;br /&gt;
            color: [133, 187, 101, 185], &lt;br /&gt;
            scale: [0.8, 0.8], &lt;br /&gt;
            outline: true&lt;br /&gt;
        });&lt;br /&gt;
    }&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
mp.events.addDataHandler(&amp;#039;cash&amp;#039;, (entity, newCash, oldCash) =&amp;gt; {&lt;br /&gt;
    if (entity &amp;amp;&amp;amp; entity.remoteId === localPlayer.remoteId &amp;amp;&amp;amp; newCash !== oldCash) { // Make sure the money update is for the money operation requester since addDataHandler triggers on any entity synced variable change&lt;br /&gt;
        currentMoney = newCash;&lt;br /&gt;
    }&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Local Entity variables ==&lt;br /&gt;
You can also set variables to entities for local scope like on server-side or client-side only to help protect sensitive data from public if needed.&lt;br /&gt;
{{ServersideCode|&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
mp.events.add(&amp;#039;playerReady&amp;#039;, (player) =&amp;gt; {&lt;br /&gt;
  player.whatever = &amp;#039;something&amp;#039;; // that initializes an object property to player that can be used serverside only&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{ClientsideCode|&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  mp.gui.chat.push(&amp;#039;Scope Test: &amp;#039; mp.players.local.whatever); // Displays: &amp;quot;Scope Test: undefined&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
== Final notes ==&lt;br /&gt;
Entity variables are destroyed once the entity is no longer available in the world. Like player reconnecting or quit or vehicle delete.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
{{ScriptingTutorials}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>imported&gt;SugarD-x</name></author>
	</entry>
</feed>