Getting Started with Entity variables
Getting Started
In this page you'll learn about how to use Entity variables. This is a list of functions that are related to the entity variables:
- Functions:
- [getVariable](/entity-getvariable)
- [setVariable](/entity-setvariable)
- [addDataHandler](/events-adddatahandler)
What is entity variables?
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.
Synced entity variables
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. Let's say you need to keep your scoreboard updated every 3 seconds and it displays every player's cash, ping, job, and etc. To keep things up to date you'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's synced variable. There are many applications for entity synced variables and you can explore [resources](https://rage.mp/files/category/3-scripts/) to get to know more about it.
Example
We'll do a short example about money HUD and how it works with entity synced variables.
Firstly we'll setup our server-side simple money API
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.
Now on client-side we should do a small money HUD that'll display the cash and update once an operation has been made. NOTE: [addDataHandler](/events-adddatahandler) triggers on all clients when one entity updates its synced variable, so make sure to specify your scope if its per player operation.
Local Entity variables
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.
Final notes
Entity variables are destroyed once the entity is no longer available in the world. Like player reconnecting or quit or vehicle delete.