Hello!
I am trying to make the Max Health of a player increase as they level up. I've included the entire leaderboard script below, All of it works fine when I remove the Health portion of the script, but I'd really like to get that part functioning too. I've approached it several different ways, generally getting the output that "Health" or "Humanoid" or "plr" is not a valid part of Workspace.
Any advice would be much appreciated!
The problematic part of the script:
local Health = game.Workspace.plr.Health local MaxHealth = game.Workspace.plr.Health * level.Value + 10
The entire leaderboard script:
game.Players.PlayerAdded:connect(function(plr) local stats = Instance.new('IntValue', plr) stats.Name = 'leaderstats' local points = Instance.new('IntValue', stats) points.Name = 'Points' points.Value = 0 local experience = Instance.new('IntValue', stats) experience.Name = 'EXP' experience.Value = 0 local level = Instance.new('IntValue', stats) level.Name = 'Level' level.Value = 0 -- The two lines below is where it breaks local Health = game.Workspace.plr.Health local MaxHealth = game.Workspace.plr.Health * level.Value + 10 -- The two lines above is where it breaks experience.Changed:connect(function() local required=(level.Value*70) -- Number reflects the EXP points needed to level up. if experience.Value >= required then level.Value=level.Value+1 experience.Value=experience.Value-required end end) end)
The error is because 'plr' is not a member of workspace, it is instead a player object. Also, 'Health' is not a member of plr.
If you want to to implement this you are going to need to get the character of the player and set the health of that character'a humanoid to the desired max health. But theirs another catch, as this value will be reset each time the character respawns. To fix that we can use the CharacterAdded event and set the health each time the character respawns.
We also want to force MaxHealth to update every time the value changes as well. To do that we can use the changed event.
As replacement to your two problematic lines, try something like this:
If plr.Character and player.Character:FindFirstChild("Humanoid") then plr.Character.Humanoid.MaxHealth = 100 + level.Value*10 end level.Changed:connect(function(value) if plr.Character and player.Character:FindFirstChild("Humanoid") then plr.Character.Humanoid.MaxHealth = 100 + level.Value*10 end end) plr.CharacterAdded:connect(function(character) character:WaitForChild("Humanoid").MaxHealth = 100 + level.Value*10 end)
Assuming your variables are all defined - we'd connect the setting of the humanoid's health continuously by setting a connection.
level.Changed:connect(function() MaxHealth = game.Workspace.plr.Health * level.Value * 10 end)
You can then use the variable to set the player's health on another line.
If you want to use the same variables, you would have to replace it with:
local Health = game.plr.localPlayer.Humanoid.Health local MaxHealth = game.plr.localPlayer.Humanoid.Health * level.Value + 10
I think this should work, since I've only done this for a couple of weeks, but I see many people do this. Try it out!