So I've finally got this script down. It's a simple script that levels you up based on if you have the correct experience. Now the problem is:
Say you have 180/200 experience. And then you gain 50 more. I have implemented line 15 to subtract the exp threshold from the experience you have. So when you're at level 2, and gain 50 exp at 180/200, you level up to level 3 and now have 30/300 exp.
Now that line works like a charm, but the problem is: say you have 180/200 exp, and then you gain 500 more exp. The script will subtract 200 when you level up and you will be left with a player who is level 3 with 480/200 exp. I can't figure a way around it. Any help is appreciated. Thanks for reading.
game.Players.PlayerAdded:connect(function(plr) local stats = Instance.new('IntValue', plr) stats.Name = 'leaderstats' experience = Instance.new('IntValue', stats) experience.Name = 'EXP' experience.Value = 0 level = Instance.new('IntValue',stats) level.Name = 'Level' level.Value = 0 experience.Changed:connect(function() local required=(level.Value*100) if experience.Value>=required then level.Value=level.Value+1 experience.Value=experience.Value-required end end) end)
You can fix this problem by calling the same function again if the player levels up using a nested function. Try the code below, and tell me if it works.
game.Players.PlayerAdded:connect(function(plr) local stats = Instance.new('IntValue', plr) stats.Name = 'leaderstats' experience = Instance.new('IntValue', stats) experience.Name = 'EXP' experience.Value = 0 level = Instance.new('IntValue',stats) level.Name = 'Level' level.Value = 0 end)--edited local function level() local required=(level.Value*100) if experience.Value>=required then level.Value=level.Value+1 experience.Value=experience.Value-required level()--if player levels up, then check for level up again end end experience.Changed:connect(level)
EDITS I forgot to add an end, sorry about that!
The function that checks the experience against the amount for leveling only fires when the experience value changes. Run it just after the character levels up too to ensure the proper number of levels are achieved.