I have made a script for my RPG game were it changes the health of the player according to the level of the player. It works, but there's a problem. When a player is in level 1, the player has 100 health, when he supposed to have 50. It only fixes itself when the player resets. What did i made wrong here?
--------------- --[Variables]-- --------------- local player = script.Parent.Parent local stats = player:WaitForChild("leaderstats") local lvl = stats:WaitForChild("Lvl") --------------- --[Functions]-- --------------- --Change health when a character is added according to the players leve player.CharacterAdded:connect(function() repeat wait() until lvl if lvl.Value == 1 then player.Character.Humanoid.MaxHealth = 50 player.Character.Humanoid.Health = 50 else player.Character.Humanoid.MaxHealth = lvl.Value*50 player.Character.Humanoid.Health = lvl.Value*50 end end) --Change health when the player levels up according to the level lvl.Changed:connect(function () repeat wait() until stats if player.Character ~= nil then if player.Character:findFirstChild("Humanoid") then player.Character.Humanoid.MaxHealth = lvl.Value*50 player.Character.Humanoid.Health = lvl.Value*50 end end end)
repeat wait() until lvl
and
repeat wait() until stats
Do nothing!
Also on line 27, you should check if FindFirstChild returns nil.
Additionally, you only need to increase the character's health while they are alive, while they are alive! That may sound redundant, but I am trying to make you understand you can put that connect function inside of the other one.
--------------- --[Variables]-- --------------- repeat wait() until game.Players.LocalPlayer; local player = game.Players.LocalPlayer local stats = player:WaitForChild("leaderstats") local lvl = stats:WaitForChild("Lvl") --------------- --[Functions]-- --------------- -- Change health when a character is added according to the players level player.CharacterAdded:connect(function(Character) if lvl.Value == 1 then Character.Humanoid.MaxHealth = 50 Character.Humanoid.Health = 50 else Character.Humanoid.MaxHealth = lvl.Value * 50 Character.Humanoid.Health = lvl.Value * 50 end -- Change health when the player levels up according to the level lvl.Changed:connect(function () player.Character.Humanoid.MaxHealth = lvl.Value * 50 player.Character.Humanoid.Health = lvl.Value * 50 end) end)
Also, correct me if I am wrong, but you don't need to create a separate check for making their health 50 if they are level 1, because their health is supposed to be 50 * level anyway?
--------------- --[Variables]-- --------------- repeat wait() until game.Players.LocalPlayer; local player = game.Players.LocalPlayer local stats = player:WaitForChild("leaderstats") local lvl = stats:WaitForChild("Lvl") --------------- --[Functions]-- --------------- -- Change health when a character is added according to the players level player.CharacterAdded:connect(function(Character) -- When the player spawns, change their health to 50 times their level Character.Humanoid.MaxHealth = lvl.Value * 50 Character.Humanoid.Health = lvl.Value * 50 lvl.Changed:connect(function () --When the player levels up, increase their maxhealth and heal them fully player.Character.Humanoid.MaxHealth = lvl.Value * 50 player.Character.Humanoid.Health = lvl.Value * 50 end) end)
EDIT Assuming you have ResetGuiOnSpawn Enabled, you don't need to connect when a new character is added.
--------------- --[Variables]-- --------------- repeat wait() until game.Players.LocalPlayer; local player = game.Players.LocalPlayer local stats = player:WaitForChild("leaderstats") local lvl = stats:WaitForChild("Lvl") --------------- --[Functions]-- --------------- -- When the player spawns, change their health to 50 times their level Character.Humanoid.MaxHealth = lvl.Value * 50 Character.Humanoid.Health = lvl.Value * 50 lvl.Changed:connect(function () --When the player levels up, increase their maxhealth and heal them fully player.Character.Humanoid.MaxHealth = lvl.Value * 50 player.Character.Humanoid.Health = lvl.Value * 50 end)