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)

1 comment
Thanks for the help. The script works, but doesn't work when a level 1 player joins the game, and starts working only when that player dies.