How to add remaining XP to XP level

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I’m trying to make a level-up system and I’ve run into a problem where If the amount of XP you gain is higher than the Max XP then it doesn’t add the remaining XP. So an example is that our XP is 180/200 and we gain 60 XP and our new XP is 0/200 but we didn’t gain the remaining 40 XP that we need.

1 Like

lets say you have 180 / 200 xp and you gain 60. You want to do something like this

local gainedXP = 60
local maxXP = 200
local currentXP = 180
local xpDifference = maxXP - currentXP

local leftOverXP = gainedXP - xpDifference 
local function FunctionThatLevelsYouUp()
if levelUp then
currentXP = leftOverXP
end
end

(Keep in mind this is hypothetical code, do not just copy it. I gave you what logic you should use to find out the leftover XP after the level up.

Try something like this

local level = 1
local XP = 180 -- current XP
local Max_XP = 200

local XP += 40 -- XP increase

if XP >= Max-XP then
	XP = XP - Max_XP
	level += 1 -- Level change
end

The XP will immediately be set to the difference between the XP and the max xp.

2 Likes