Need Help with health bar

So, I’ve created a health bar, a stat system, all that stuff… but when your health stat levels up you get 20+ max health, now say you’re at level 4 health and you have 180 health. You spawn in with 100/180 health and then start healing up-to 180. How do I fix it? I tried doing this but it didn’t work:

game.Players.PlayerAdded:Connect(function(Player)
	local Char = Player.Character
	Char.Humanoid.MaxHealth = Player.Stats.HealthLevel.Value * 20 + 80
	Char.Humanoid.Health = Char.Humanoid.MaxHealth
end)

4 Likes

You should be connecting the script to .CharacterAdded since you may be trying to edit the health of the humanoid before it exists.

Ah-
I’m stupid, lemme try that

Didn’t work ;-;
Should I move it to a local script and change the code to this:

game.Players.LocalPlayer.PlayerAdded:Connect(function(Player)
	local Char = Player.Character
	Char.Humanoid.MaxHealth = Player.Stats.HealthLevel.Value * 20 + 80
	Char.Humanoid.Health = Char.Humanoid.MaxHealth
end)

Would adding local player make a difference?

Ooookay…
I just somehow fixed it, I just put wait(4) when the player joins and it worked

Use task.wait instead of wait, its deprecated

1 Like

Alright, thanks I’ll try that.

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