Connecting Player Health to Stats

Hellooooo, heres another post on something that I know is simple but can’t figure out for some reason! What I am doing is having a leaderstats script that has a “Health” stat… Later I will add a buyable thing that allows the player to buy more health, the health that the player buys will be added to the Health stat, which is connected to the players actual health. What I’m now having trouble with is making it so that the players max health becomes the value of the health stat whenever it goes up… if that makes sense.

I tried making a few scripts, but I think my problem is identifying the humanoid?

game.Players.PlayerAdded:Connect(function(Player)
	Player.Humanoid.Health = Player.hiddenstats.Health.Value
end)

If anyone can help pleasee do! I feel like Ive done a bad job explaining also as a lot of repetitive wording so if you dont understand please ask ill try to explain more lol.

Edit: The script above is a script i tried a few times in a normal server script in workspace, gives me an error saying humanoid isnt valid.

1 Like

Make an Event using Player.CharacterAdded,which gives you the character when they spawn in.
From that event, grab the Character, and look for the Humanoid, when found use the HealthChanged Event located in the Humanoid.

2 Likes

Ok may or may not have had to watch a video for that.
I came up with this is that what you meant?

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		Character.Humanoid.Health = Player.hiddenstats.Health.Value
	end)
end)

didnt work so im obviously missing something :joy:

1 Like

That would be right, all you need to do next is to create an Event HealthChanged

like so:

-- CharacterAdded Event Firing
Humanoid.HealthChanged:Connect(function() -- fires whenever the Players Health changes
    -- change health
end)

1 Like

Thanks! i still got a lot to learn hehe.

It does say that Humanoid is not known though… does it need to be defined???

1 Like

You can Define it by saying local Humanoid = Character.Humanoid, or just Character.Humanoid if you plan on using it only once.

1 Like

Still doesnt work, this is what i have. In workspace still… i tried it in a few other places also.

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		Character.Humanoid.Health = Player.hiddenstats.Health.Value
		Character.Humanoid.HealthChanged:Connect(function()
		end)
	end)
end)
1 Like

Because you are not putting it in right place, you are supposed to Apply the Change within the HealthChanged Event, Otherwise it will just be applied everytime the Player Respawns

ServerScriptService, so your code isn’t stolen.

1 Like

Ok, I think I got it, just now I believe the health stays at that health so the player doesnt take any damage.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.