Math formula help (Experience convert to Levels)

I’m struggling to come up with a formula to convert xp into levels.


I want the experience of the next required level to be *1.1 of the previous amount of xp it took to level up.

to go from lvl 1 → 2 would be 100 xp.

lvl 2 → 3 would be 110 xp.

lvl 3 → 4 would be 121 xp.

etc.

I want to store data on the player’s exact xp amount, but I’m not good enough at math to figure out what “level” they are at with math.
The only important parameters here are that lvl 1 starts at 0 xp, and level 2 starts at 100 xp.

I wouldn’t care about math but use a small script :slight_smile:
I’d use a table to store all possible levels (maxLevel) with the lower XP limits of the levels (levelLimit).
Then a simple function to search for the actual level from XP (getLevel).
This is a small demo for this, it spams random XPs with the proper level values.

local levelLimit={}
local maxLevel=10

levelLimit[1]=0
levelLimit[2]=100

for i=3,maxLevel do
levelLimit[i]=math.floor(levelLimit[i-1]*1.1)
end

local function getLevel(xp)
local lvl

for lvl=maxLevel,1,-1 do
	if levelLimit[lvl]<=xp then
		return lvl
	end
end
return 0

end

while wait(1) do
local xp=math.random(1,levelLimit[maxLevel])
local lvl=getLevel(xp)
print (xp," xp = level ",lvl)
end

1 Like

I wanted to avoid a brute force method like this

lvl = math.max(math.floor(math.log(xp/100) / math.log(2.1) + 2), 1)

I like math solutions too, thanks for this!
I checked this solution, but it gives me level 1 and 2 for all xp.
116 xp = level 2
175 xp = level 2
25 xp = level 1
22 xp = level 1
47 xp = level 1
132 xp = level 2

1 Like

Just adding some explanation to the above:

Experience = BaseLevelCost * (Multiplier ^ (Level-2))

Exp/LevelCost = Mult ^ (Level-2)

At this point we have to use logarithms to move level out of the exponent. logX(Y) is what power do you need to raise X by to reach Y. Therefore logX(X) = 1, logX(X^2) = 2, etc. So logmult(Mult^Level) = Level. Roblox doesn’t actually let us use logarithms of any base, so we have to use a change of base formula:
logX(A) = ln(A) / ln(X)

math.log(Exp/LevelCost)/math.log(Mult) = Level - 2

Then because we don’t want decimals:

Level = math.floor( 2 + math.log(Exp/LevelCost)/math.log(Mult)  )

Then because this will also go backwards into negative levels when your exp is less than the BaseLevelCost, add

Level = math.max(Level, 1)

Finally, Log(0) is undefined, so add a check that just returns 1 if the player has 0 exp.

Where BaseLevelCost is the exp required for level 1->2 (100) and Multiplier is how much it increases per level (x1.1).

Alternatively just log the player level and subtract the amount of xp required for that level each time they level up.

This doesn’t seem to give the results I’m looking for for some reason.

Level 2 is 100 xp, level 1 is 0 xp, not sure if that’s part of the consideration

For some reason I seem to be about a multitude of 10 off, not sure if it’s an error on my part. Can you substitute your formula with what you think should be in it?

5000 xp seems to give around the amount that would be correct for the amount needed between lvl 43 and lvl 44. But that’s for “amount of xp needed for next level”, not a reflection of “level based off amassed total xp”

1 Like

Let me recheck my working, I’ve clearly made a mistake somewhere.

Everyone else seems to have made similar mistakes. I’m tweaking with input values and I either get level 7 or level 43 for 5k xp, both of which are way off.

Level 7 should be (I calculated manually) 1143.58881 xp. (1.1k)

I think I put something wrong into my calculator, I wrote it out in Lua and it worked fine

local EXP = 100*(1.1^(8-2))  print( 2 + math.log(EXP/100)/math.log(1.1))

Prints 8 for example

Well how much xp are you making it try to calculate a level for?

Exp_To_Reach_Level = 100*(1.1^(Level-2)) from what you wrote in the OP

I’m testing the formula by giving it 5000 xp to see if it lands roughly where i’d expect it

I realised the mistake we both made. The total exp required is actually this summation
15759022654533940768931872116852|375x500
I don’t have a good way to simplify that on hand. I’d recommend just making a function that uses an iterative loop like suggested above, or by subtracting the exp needed for the level from the total exp each time they level up and storing the result and the level.

Yeah I might need to do that, didn’t realize this was actually more complicated than I thought

(-math.log(11) - math.log(n/1000))/(math.log(10) - math.log(11)) + 1

This returns a number, so you probably want to do math.floor to check what level you are certain to pass by getting that much experience. Also it only works for n>=92, for any value below it returns something negative (you could probably just add in a check that if it’s negative, they are level 1).