# Help with xp formula

So I am making a game that uses XP. I’m having trouble with a working XP system. It gives me 1 257500XP 100/257500 = level 1 and I don’t know why.

Here is my XP to level calculator

``````function levelCalculator(player)
if getSessionData(player) < 100 or getSessionData(player) == nil then
return 1;
else
local terms = tonumber(getSessionData(player));
return math.floor((-75 + math.sqrt(75^2 + 4 * 25 * terms)) / (2 * 25));
end
end
``````

Here is my Level to XP calculator

``````function LevelToXPCalculator(player)
local currentXP = getSessionData(player);

return currentXP / 2 * (2 * 100 + (currentXP - 1) * 50);
-- Increment 50 each time
end
``````

Could you guys help me identify the error or link me to better resources for XP?
Thank you.

2 Likes

Which function is the one that prints 257500?

LevelToXPCalculator(player) returns 257500

This looks extremly over-complicated and confusing.

Is LevelToXPCalculator supposed to return the amount of exp needed to level up at any given level?
What is levelCalculator?

level calculator converts XP into level (e.g 100 XP = Level 1)

LevelToXPCalculator converts Level back into XP (e.g 1 = 100 XP)

I’d suggest saving exp and level separately.
Could you do that?

If so then defining exp per level would be much simpler:

``````local START_EXP = 10
local EXP_PER_LEVEL = 5

function getExpFromLevel(level)
return level * EXP_PER_LEVEL + START_EXP
end
``````

That’s a simple linear exp curve but you can edit it however you’d like, it was just an example.

Thanks. Also, any ideas on how I could convert XP into level?

You just find the inverse of the function.

`y = a * b + c`
`y - c = a * b`
`(y * c) / b = a`

``````local function getExpFromLevel(level)
return level * EXP_PER_LEVEL + START_EXP
end

local function getLevelFromExp(exp)
return (exp - START_EXP) / EXP_PER_LEVEL
end
``````
2 Likes

Total exp to level would be:

``````local totalExp = 0
for level = 1, currentLevel - 1 do
totalExp += getExpFromLevel(level)
end
``````