I posted a question a while back on why my Level Up script wasn't working, and someone basically rewrote me a whole script from scratch, but didn't explain how it works.
I'm not sure how it works, and actually I don't think it does work. If someone could explain IN DETAIL how it works, especially the part I've highlighted in comments, I would really appreciate it. Thanks!
local xps = {0,3, 6, 10, 15, 20, 25, 30, 40, 50, 65, 80, 95, 110, 125, 140, 155, 180, 210, 250, 300, 350, 400, 450, 500, 575, 650, 725, 825, 1000}; -- Just a list (divided by 100 to be smaller) function experience() Player = game.Players.LocalPlayer; local xp = Player.Exp.Value; for level = 1, #xps do if xp >= xps[level] * 100 then --I'm not sure what the square brackets do here Player.leaderstats.PlayerLevel.Value = level; --I've never seen semicolons used in Rbx Lua? end end Player.HP.Value = 200 + 100 * Player.leaderstats.PlayerLevel.Value; end function health() Player.Character:WaitForChild("Humanoid").MaxHealth = Player.HP.Value end while true do --This was my attempt at looping the functions, I'm not sure if it works like this though. experience() health() end
I tried explaining some of the stuff that might be confusing. Also as a side note, semicolons are not necessary; some people just like to use them. No idea why.
local xps = {0,3, 6, 10, 15, 20, 25, 30, 40, 50, 65, 80, 95, 110, 125, 140, 155, 180, 210, 250, 300, 350, 400, 450, 500, 575, 650, 725, 825, 1000}; function experience() Player = game.Players.LocalPlayer; local xp = Player.Exp.Value; for level = 1, #xps do --[[The square bracket gets a certain item out of a table. If it were xps[1] it would get the 1st item; xps[2] the second item, and so on. Starts at level one, and goes through all the xps associated with each level.]] if xp >= xps[level] * 100 then --If the player's xps is greater than the required amount, it levels them up. Player.leaderstats.PlayerLevel.Value = level; end end Player.HP.Value = 200 + 100 * Player.leaderstats.PlayerLevel.Value; --Sets their health. If they have a higher level, they get more health. end function health() Player.Character:WaitForChild("Humanoid").MaxHealth = Player.HP.Value --This sets their health in the humanoid. end while true do experience() health() wait() --You need a wait or else it will freeze. end
The experience function takes your experience value in a player and if the value is greater than or equal to one of the xps values, it makes the player's PlayerLevel the level it is equal to. The square brackets are a necessity of the for loop, simce it is iterating over the table xps. The function then sets the player's HP value to 200 + (100*the-player's-level-value).
I do not believe the semicolon is necessary.
The heath function sets the player's MaxHealth to the HP value, implying that you should cal, the health function after the experience function.
I would not suggest using a while loop to call the functions. Instead, you can do something like
game.Players.LocalPlayer.Exp.Changed:connect(function() experience() health() end)
I believe this would call the two functions when your Exp value is changed. If you use a function like this, it causes a lot less lag then the while loop you have.
I hope this helped you! Good luck with your game!