Integration

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.

local statsInited = false

local function setupStats() 
    if not statsInited then
        statsInited = true

        -- ❗❗❗ This is the place you will add the triggers to create categories, stats and achievements ❗❗❗
    end
end

AddEventHandler('rcore_stats:api:ready', setupStats)

CreateThread(function()
    if GetResourceState("rcore_stats") == "started" or GetResourceState("rcore_stats") == "starting" then
        while not statsInited do
            TriggerEvent('rcore_stats:api:isReady', function(isReady)
                if isReady then
                    setupStats()
                end
            end)
            Wait(1000)
        end
    end
end)

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 separately

  • server - the server stats are basically accumulators of player stats

    • for example, if you have a stat distance_car, you can create stat total_distance_car and the script will automatically sum all distance_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 mph

  • distance - the stat value should be in meters and it will be displayed in km or mi

  • finance - this stat will be displayed as $xxx if not configured to something else in configs/config_units.lua

  • time - this stat should be in minutes and will be displayed as 3h, 23m for example

  • nil - 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

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.

🥳 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

Was this helpful?