Leveling system issues

Hi,

So i made an Leveling system. Currently when player have more than needed amount of exp i just add 1 level and set exp to 0. I want to make it so if to level up i need 10 exp (im level 1), but i just got 30 exp and i use for example this formula to get needed exp to level up - Level * 10, so it should add me 2 levels and leave 10 exp and to level up i need 30

Heres how its currently:

if lvl * 10 <= newexp then
	DataService:Set(plr, "Level", lvl + 1)
	newexp = 0
	DataService:Set(plr, "Exp", newexp)
end

What i have tryed so far:

if lvl * 10 == newexp then
	DataService:Set(plr, "Level", lvl + 1)
	newexp = 0
	DataService:Set(plr, "Exp", newexp)
elseif lvl * 10 < newexp then
	local newlevel = lvl
	repeat
		newexp = newexp - newlevel * 10
		newlevel = newlevel + 1
	until lvl * 10 > newexp 
    DataService:Set(plr, "Level", newlevel)
    DataService:Set(plr, "Exp", newexp)
end

It supposed to give get how much levels i get for this amount of exp and if exp no more meet requirements it will stop and set the data i have got.
For some reason it did something weird so i decided to make it simple just like i shown above.

Can someone good at math tell if this is good or what is bad? Maybe i just tested it bad and actually it was doing good?

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Update: I tested it again and sometimes it sets -X Exp.
Useful Info: Level i had when tested: 3; Exp i had when tested: 18; Exp Needed to lvl: 30 (using formula i gave)

Well your problem is that the code clears the XP after a level up. Instead, rewrite it so that it subtracts the XP required to level up from the players XP and levels them up again.

So say you had 35 XP, and your on level 1. Level 1 needs 10 XP, level 2 needs 20. Your code will update them past level 1, so their level goes to 2 and their XP goes to 0. You need to rewrite it so your code will take their 35 XP, recognize they can pass level 1 with 10 XP, subtract 10 from 35 leaving them with 25 XP. Check the next level now to see if they can pass that. 25>20 so they can. Update their level, and set their XP to 25-20. They are left with 5 XP.

Isn’t im making it just like you said right here? If XP = XP needed to level up, i will set it to 0 and add only 1 level. If XP > XP needed then i will repeat removing needed XP to certain level until XP will be less then needed?

Oh wait i need to do:

until newlevel * 10 > newexp 

and not

until lvl * 10 > newexp 

I will test it

Update: Looks like it work now.