# Faster way than Repeat Until?

Is there’s any faster way than this? i want to make an Level System in my Game, so basically everytime you level up your Experience Requirement will be Increased by 500, so if you’re still level 1 and you got 2000 Experience your level will increase by 2 Levels.
if i use Division then the player will increase their level by 4 not 2 so i use this:

``````local total = 0
repeat
wait()
total = total + 1
expreq.Value = expreq.Value + 500
until exp.Value < expreq.Value
exp.Value = exp.Value - expreq.Value
level.Value = level.Value + total
expreq.Value = level.Value * 500
points.Value = points.Value + (5*total)
``````

I added wait() because it will crash the game if the players got too much exp, like 100M exp at level 1 will crash the game, but now if you got that much exp you must wait a really long time just to level up, which i really don’t want, so maybe there’s an algorithm to these kinds of thing without repeating things. and still got accurate results. if there is, please tell me!

Yeah, there’s definitely a better way to do this. You can just round the difference to the nearest 500. Something along the lines of this would work:

``````local diff = exp.Value - expReq.Value
local total = math.ceil(diff / 500)

exp.Value = exp.Value - (total * 500)
level.Value = level.Value + total
expReq.Value = level.Value * 500
points.Value = points.Value + (total * 5)
``````

Basically, this just finds the difference between the numbers and finds what 500 would have to be multiplied by in order to be greater than (or equal to) it.

2 Likes

so, it works like this?
the exp is increased by 2000, player level is still level 1 and exp requirement is 500.
then it finds how much we should multiply it to reach the exp value?
i tried your method, but when i add 2000 exp to the player the level increased by 3, not 2, but when i try killing a monster that gives 500 exp 4 times my level increased by 2 with a 500 exp left.

so i just try my old method. but rather than changing the values i make a variable.
the results is:

``````local total = 0
local cexp = exp.Value
local cexpreq = expreq.Value
repeat
cexp = cexp - cexpreq
cexpreq = cexpreq + 500
total = total + 1
until cexp < cexpreq
level.Value = level.Value + total
expreq.Value = level.Value * 500
exp.Value = cexp
points.Value = points.Value + (5*total)
``````

it doesnt break the game when i try adding a lot of exp but it freezes for a little bit, like really short freeze about 0.1 seconds.
but it’s accurate. but still. it freezes for 0.1 second so im still searching for a better way to do this.
btw, the script goes like this:
a player exp is increased.
the script checks if it’s higher than exp requirement, if it’s higher then.
the script will make a variable storing the exp and exp requirement values
then the script will repeat this:
exp req increased by 500 and exp decreased by exp req ( the ones that decreased is the exp variable btw)
it repeats that until exp value is smaller than exp req
then all those repeats is stored in total and increase the player level,points,etc. by the amount of totals.(somehow when my stats is e+ it breaks the whole script, probably because of too much math lol)

I’d try to simplify your game logic to fit with a math function that is easier to understand and explain.

For example, you can double the experience requirement at every level.

level = math.log(exp)/math.log(2)

eg:

``````print(math.log(16)/math.log(2))
print(math.log(32)/math.log(2))
print(math.log(128)/math.log(2))
print(math.log(90000)/math.log(2))
``````

There are other things you can do, of course. The key is to simplify your logic.

However, if you still want to use your logic, here’s a nice little trick

query - “y=500+1000+1500+…+n, solve for n”

Results in

``````eg:
exp=500 print(1/50*(math.sqrt(10*exp+625)-25))
exp=1500 print(1/50*(math.sqrt(10*exp+625)-25))
exp=3000 print(1/50*(math.sqrt(10*exp+625)-25))
``````

But, learn your algebra. Wolfram is a risky crutch

1 Like