Humanoid.HealthChanged doesn't seem to work after the player dies and respawns

I’m working on a custom Healthbar that works fine when I load in but when I die and respawn it doesn’t detect a change in the humanoids health anymore.

I have other events within this same local script working fine at all times it’s just this specific event that doesn’t work after a player respawns

hum.HealthChanged:Connect(function(dmg)
     print("HUM HEALTH CHANGED")
     Health:WaitForChild("Bar").Size = UDim2.new(dmg / hum.MaxHealth,0,1,0)
 end) 

Is the HealthChanged event nested within a CharacterAdded event?

1 Like

This could be because, potentially it stops listening for this event once the player has died. Therefore I would opt for this approach that I wrote to answer this question.

local player = game.Players.LocalPlayer

player.CharacterAdded:Connect(function()
	if player.Character then
		-- now start listening for health changed
		local humanoid = player.Character.Humanoid
		
		-- however obviously you want to update the health and not wait for it to update so the bar doesn't look strange
		
		Health:WaitForChild("Bar").Size = UDim2.new(humanoid.Health / humanoid.MaxHealth, 0, 1)
	
		humanoid.HealthChanged:Connect(function(dmg)
			Health:WaitForChild("Bar").Size = UDim2.new(dmg / humanoid.MaxHealth, 0, 1, 0)
		end)
	end
end)
3 Likes

I believe it is because when you respawn, it gives you a new humanoid. So you must nest it in a CharacterAdded event.

2 Likes