Integration
This page is for script developers and server maintainers who want to integrate rcore_stats into their scripts.
You do not need to do anything here to enjoy the full functionality of rcore_stats on your server.
Here you will find all the necessary information to integrate rcore_stats into your script.
Creating categories, stats and achievements
Everything related to creation is SERVER SIDE only.
Every event is "ensure" event, which means that it will create the category/stat/achievement only if it doesn't exist yet. That makes it easier for you as you can just call this event every time your resource starts and you don't have to worry about creating the same category/stat/achievement multiple times.
To create these data you should do it in a particular order, that's why we have prepared this part of documentation as step-by-step guide.
Step 1: Make sure the rcore_stats script is ready
rcore_stats emit an event when they are ready, but just in case your integration script is loaded before rcore_stats, you can check if the script is ready with this code:
👉 This part is required to detect if rcore_stats API is ready and to create all things needed.
Step 2: Create a category or use existing one
Available categories by default: distance
, finance
, speed
, favorite
, combat
, other
If your desired statistic doesn't fit into any of these categories, you can create your own category.
Step 3: Create a stat type
You can create two types of stat types: player
and server
.
player
- this type of stat is calculated for each player separatelyserver
- the server stats are basically accumulators of player statsfor example, if you have a stat
distance_car
, you can create stattotal_distance_car
and the script will automatically sum alldistance_car
stats for all players so you can see the total distance traveled by all players
There are also unit types that can be used to display special stat types, like speed, distance etc.
Available unit types are:
speed
- the stat value should be in meters per second and it will be displayed in km/h or mphdistance
- the stat value should be in meters and it will be displayed in km or mifinance
- this stat will be displayed as $xxx if not configured to something else in configs/config_units.luatime
- this stat should be in minutes and will be displayed as3h, 23m
for examplenil - if you use nil, no unit type will be used and stat will be displayed as is
Step 3.1: Create a player stat type
If you want to create a stat type with a custom category that you just created above, you need to create the category first and to be really sure, you can use the last parameter in create category event as a callback and then create the stat type in the callback.
Step 3.2: Create a server stat type
Server stats are accumulators of player stats, so you can see the total value of all player stats of the same type.
Step 4: Create an achievement
Achievements are a way to reward players for reaching a certain goal. You can create achievements for player stats only.
Each achievement has an icon, default is 🏆, but you can set any icon from https://lucide.dev/icons/.
Simply go to the page, find the icon you like, click on it and copy the name, for example loader-pinwheel
.
If you want to create an achievement for a stat type that you just created above, you need to create the stat type first and to be really sure, you can use the last parameter in create stat type event as a callback and then create the achievement in the callback.
🥳 Congrats! You can now easily integrate statistics and achievements into your script!
But wait, you may ask, how can I change the stats of the player?
Changing stats
Changing stats is really easy, you just need to call an event and you're done!
Everything else will be handled for you, the script will increase/decrease/set the value of the stat type, check if the player has reached any achievements and save the data to the database.
All changes in stats are handled on CLIENT SIDE!!!
Increase stat value
Find the place in your script where you want to increase the value of the stat type and call this event:
Decrease stat value
Find the place in your script where you want to decrease the value of the stat type and call this event:
Set stat value
In some cases, you may want to set the value of the stat type to a specific value, for example if the value should not be a number, but a string (text). Or if you want to reset the value of the stat type to 0.
🥳 Now your integration should be ready to go, enjoy!
Last updated