Hey so I am making a Levels system and I ran into a problem when a player wants to buy experience. If they buy, let’s say 50,000XP, it would overflow the RequiredXP to gain to the next level, and the process will have to repeat until the XP is not overflowing which will take alot.
Is there a way to calculate the level that reaches up to 50,000XP ?
I tried using this equation but so far I got nothing.
600(1.5)^x-1 = 50,000
-- Initial Required XP * (Required XP Increase rate after level up) ^ (level - 1) = 50,000
I’m not exactly sure what you are trying to do, but in the comment you have (Required XP Increase rate after level up) ^ (level - 1), but in the code you just have (1.5)^x-1, which is the same as ((Required XP Increase rate after level up) ^ level) - 1.
If Im understanding correctly basically you want the XP to keep being given until there is no more, so your going to need some sort of while loop to keep leveling up the player with the xp you gave them until there is none left to give
Get the players current level
local CurrentLevel = …
Set an increment counter
local LevelsEarned = 0
Get the XP count required for the Next Level
where NextLevel = CurrentLevel+LevelsEarned+1
Subtract XP for level from increment that starts with XP total
XpUsed = …
If XpUsed > 0 then
While there is still XP, increment LevelsEarned by 1, and use that to get the price for next level, subtract that from the XpUsed counter, and repeat until LevelsEarned = total number of levels you can get from initial value of XpUsed and XpUsed < 0?
Make sure not to count the last one which goes under.
This is assuming you want to calculate how many levels a certain amount of XP would give without actually applying it. If not, you can just remove the LevelsEarned part and increment CurrentLevel directly rather than LevelsEarned
Here you can see that the experience is overflowing vs the 600 required XP to level up, when my xp changes ill level up by one and slowly repeat the process until my required XP is less than my actual XP.
If im making sense, I dont want it to do that, I just want it to straight away level up to that certain level that adds up to the XP I have.
Sounds like you may need a while loop like dex suggested or a recursive function. Are you asking the level that reaches up to 50,000 XP straight from level 1, or to see how far a player will go with their current level? Maybe something like this? I’m a little rusty with this stuff so feel free to correct me if it’s wrong.
local sum = 0
local x = 0
while sum <= 50000 do
wait()
if sum > 50000 then break end
print(sum.." "..x)
sum += (600*math.pow(1.5, x))
x += 1
end
Output:
0 0
600 1 --1 to 2
1500 2 --2 to 3
2850 3 --3 to 4
4875 4 --4 to 5
7912.5 5 --5 to 6
12468.75 6 --6 to 7
19303.125 7 --7 to 8
29554.6875 8 --8 to 9
44932.03125 9 --9 to 10
Assuming 600 exp is the amount need to go from level 1 to 2, it seems like 50,000 is enough to satisfy reaching level 10. You’d have a remainder of ~5000 experience in between 10 and 11. This is for the total experience needed to reach a certain level from the start, not from a certain one to the next btw.