Adding too much xp wont account for all of the levels

I currently have a level system which adds xp and if the xp exceeds the needed xp to level up then it levels the player up. The problem is when you add a holy amount of xp it will only level up once and the remainder of the of the current xp - needed xp will still be a large amount. I know im missing a calculation but im too tired for this.

Code:

local NeededXp = self.Levels * 100
self.Xp += Change
		
if self.Xp >= NeededXp then
	local Remainder = self.Xp - NeededXp
			
	self.Levels += 1
	self.Xp = Remainder
end
		
print(self.Levels, self.Xp)

if anybody could leave a comment, would be helpful

try replacing

if self.Xp >= NeededXp then

with

while self.Xp >= NeededXp do

Also, within the while statement, added

NeededXp = self.Levels * 100

such that the XP needed remains accurate:

local NeededXp = self.Levels * 100
self.Xp += Change
		
while self.Xp >= NeededXp do
	local Remainder = self.Xp - NeededXp
			
	self.Levels += 1
	self.Xp = Remainder
        NeededXp = self.Levels * 100
end
		
print(self.Levels, self.Xp)
3 Likes

Thanks ill implement this in a second, though i could just do a calculation for it cant i?

If you want to remove the loop, then yes - you should be able to come up with a formula using log.
The readability of it won’t be great though, so the loop is probably sufficient.

1 Like

Thanks for your answer, im looking for a calculation i would use this and mark it as a solution if i dont find one.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.