Shared
XP System

XP System

rp-base ships a simple level/rank system based on cumulative XP stored on the player. Two conversion helpers are available on both server and client.


Formulas

Level from XP uses a 4th-root curve so early levels are fast and later ones slow down naturally:

level = ⌊xp^(1/4)⌋

XP required to reach a given level is the inverse:

xp = ⌊level^4⌋

Functions

RP.GetLevelFromXP(xp)

Returns the level for the given XP amount.

ParameterTypeDescription
xpnumberCurrent XP

Returns integer

RP.GetLevelFromXP(0)      -- 0
RP.GetLevelFromXP(1)      -- 1
RP.GetLevelFromXP(10000)  -- 10
RP.GetLevelFromXP(160000) -- 20

RP.GetXPFromLevel(level)

Returns the total XP required to reach level.

ParameterTypeDescription
levelintegerTarget level

Returns integer

RP.GetXPFromLevel(1)   -- 1
RP.GetXPFromLevel(10)  -- 10000
RP.GetXPFromLevel(20)  -- 160000

Ranks

Level is mapped to a rank using Config.Ranks. Levels are grouped into buckets of 10, with 1–10 mapping to the first rank entry, 11–20 to the second, etc.

-- server: RCPlayer:getRank()
-- client: RP.GetPlayerRank()

Both return the rank name string from Config.Ranks for the player's current level.

-- Config.Ranks example
Config.Ranks = {
    [1]  = "Newcomer",
    [2]  = "Regular",
    [3]  = "Veteran",
    -- ...
}

The rank index is calculated as:

math.ceil(math.max(1, math.min(#Config.Ranks, level / 10)))

Example: Granting XP

XP is stored directly on the player's xp column. Update it via the accounts proxy (or direct SQL) then broadcast the change through the player loaded event or a dedicated event.

-- Server
local p = RP.GetPlayerFromSource(src)
if p then
    p.xp = p.xp + 500
    p:save()
end