Leveling up Script Support

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

I will appreciate any help! Have a nice day! :smiley:

2 Likes

Is there an event, that connects to the function this is in?

1 Like

En Example:

local LevelUpEvent = game.ReplicatedStorage.LevelUpEvent

LevelUpEvent.OnServerEvent:Connect(function(Player)
    local Exp = Player.leaderstats.Exp
     etc
end)

Humanoid.health is already a intvalue
(But it’s a good idea to think about that)

I’m not sure about this, but does ; effect anything? Like, I used that in cpp, but not in the lua

Is there a mistake I made? I’m not sure…

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

1 Like

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.

You can try out the square root way of leveling, meaning Level = sqrt(EXP)*constant

So you’d do something similar to:

local constant = -- some number

EXP.Changed:Connect(function(value)
Level.Value = math.sqrt(EXP)*constant
end)

Here is an image to show how the square root graph behaves with the constant equal to one:

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 >=.

Whoops! What a silly mistake I made! I’ll fix that.

If you do that they will level every time they gain xp after 25 xp.

1 Like

No if you put if EXP.Value > 25 then yes you are correct.

No, it will give you a level everytime you gain xp once you have 25 xp

Make sure it is => 25, as they may not ever have exactly 25 exp? They may get more exp than wanted, having 26 exp?

Yeah I know I’m gonna fix that.

1 Like

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

Thanks for the info @Btkelley17!

Or just change the if statement to check if they have more than the new required xp amount instead of the old.