So basically I’m trying to make a make a game with a leveling up system, and I had some trouble with converting exp to levels. Please let me know if I made any mistakes. Here is the script:
if sucess and hum.Health>0 then
local ls = player:FindFirstChild("leaderstats");
if ls then
local Level = ls:FindFirstChild("Level");
if EXP.Value == 25 then
Level.Value = Level.Value +1
print("Leveled up!")
end
end
end
Semicolons don’t affect the runtime effect of code.
I understand the preference for them, even in Lua; I’ve had one too many debugging nightmares with anonymous functions accidentally being called and weird semantics where I figured a statement would end but doesn’t, to the point that every language I can use semicolons in, I will.
Doesn’t hurt also since you can typically attach some linter to a git hook to automatically insert them, if needed
They can be used, but most don’t use them. I typically put semicolons when typing multi-line things in the command bar, so it’s easy for me to go back and check something.
Does experience only get increased in intervals of one? If not, perhaps the check should be experience >= 25?
Also, does experience cumulate or should it be consumed? If it is cumulative, then 25 would be something more like <current level + 1> * 25. If it is consumed, then somewhere after a level is rewarded, the experience should be decremented by however much the threshold was; in this case, you would add something like experience = experience - 25.
It also must be said that experience thresholds in games tend to linearly increase with level, so I’m unsure if 25 should be used as a static threshold, never changing.
Are you increasing experience in perfect increments? This script will ONLY increase the level of the experience is EXACTLY equal to 25. If your experience goes from 20 directly to 30, for example, this will not work. To fix this, change == to >=.
Most games set xp to zero once someone levels up.
So you can use >= if you plan on setting their xp to zero. Then to transfer it, you would subtract the xp from 25 or the amt it takes to level up, then add that to their new xp
if player.EXP.Value >= 25 then
lastxp = player.EXP.Value - 25
-- level up
player.EXP.Value = lastxp
end