How to make so this script work even when player dies?

So, this script is basically a healthbar that shows different frame depending on player’s health level. I made it using answers from my previous question. The thing is, it works perfectly, but when player dies it stops working(The frames are not even showing)

local player = game.Players.LocalPlayer

local char = player.Character or player.CharacterAdded:Wait()

local human = char:WaitForChild("Humanoid")

local hud = player.PlayerGui.Hud.ImageLabel

local fine = hud.fine

local fine2 = hud.fine2

local caution = hud.caution

local danger = hud.danger

for _,player in pairs(game.Players:GetPlayers()) do

human.HealthChanged:Connect(function(Dmg)

local hp = human.Health

fine.Visible = hp >= 80

fine2.Visible = hp >= 60 and hp < 80

caution.Visible = hp >= 30 and hp < 60

danger.Visible = hp < 30

end)

end

And there’s some of the methods that don’t work:
ResetOnRespawn = true just stops the script on “danger” frame.
This script:

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		local hud = player.PlayerGui.Hud.ImageLabel
		local fine = hud.fine
		local fine2 = hud.fine2
		local caution = hud.caution
		local danger = hud.danger

		humanoid.HealthChanged:Connect(function(health)
			fine.Visible = health >= 80
			fine2.Visible = health >= 60 and health < 80
			caution.Visible = health >= 30 and health < 60
			danger.Visible = health < 30
		end)
	end)
end)

Also don’t work, the frames are not even showing too, I also tried to add WaitForChild’s as recommended, but it didn’t change anything, the script’s still aint working

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)


		local humanoid = character:WaitForChild("Humanoid")
		local hud = player.PlayerGui.Hud:WaitForChild("ImageLabel")
			local fine = hud:WaitForChild("fine")
			local fine2 = hud:WaitForChild("fine2")
			local caution = hud:WaitForChild("caution")
			local danger = hud:WaitForChild("danger")

		humanoid.HealthChanged:Connect(function(health)
			fine.Visible = health >= 80
			fine2.Visible = health >= 60 and health < 80
			caution.Visible = health >= 30 and health < 60
			danger.Visible = health < 30
		end)
	end)
	end)

UI is organized like this if needed: Screenshot_85

Go to the hud properties and turn off resets on death i think its what its called

reeeeead theeee quessstions carefullyyyyy

ResetOnRespawn = true just stops the script on “danger” frame.

turning it off does nothing, the frames are not showing

Disable the “ResetOnSpawn” property and then just script the UI to reset itself when the player spawns.

whenever you die, the character gets deleted.
But the value char will still be set to the old (now non existant) character.
To fix this, you should take an event from when a player has respawned, and whenever the event gets fired, it sets the Char value and all values associated with it to the new Character.

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		local hud = player.PlayerGui.Hud.ImageLabel
		local fine = hud.fine
		local fine2 = hud.fine2
		local caution = hud.caution
		local danger = hud.danger
		
		humanoid.HealthChanged:Connect(function(health)
			fine.Visible = health >= 80
			fine2.Visible = health >= 60 and health < 80
			caution.Visible = health >= 30 and health < 60
			danger.Visible = health < 30
		end)
		humanoid:TakeDamage(1)
	end)
end)

Here, the humanoid takes 1 damage which should cause the “HealthChanged” event to fire which will result in the UI resetting itself.

local function Update(Health, fine,fine2,caution,danger)
fine.Visible = health >= 80
fine2.Visible = health >= 60 and health < 80
caution.Visible = health >= 30 and health < 60
danger.Visible = health < 30
end

local players = game:GetService(“Players”)

players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)

	local humanoid = character:WaitForChild("Humanoid")
	local hud = player.PlayerGui.Hud:WaitForChild("ImageLabel")
		local fine = hud:WaitForChild("fine")
		local fine2 = hud:WaitForChild("fine2")
		local caution = hud:WaitForChild("caution")
		local danger = hud:WaitForChild("danger")

		Update(Humanoid.Health,fine,fine2,caution,danger)

	humanoid.HealthChanged:Connect(function(health)
		Update(health,fine,fine2,caution,danger)
	end)
end)

end)

not showing~~
Even with WaitForChild

You need to disable the “ResetOnSpawn” property. If it still doesn’t work then there’s some other issue.

Man, I did it right away, i’m pretty sure there’s some other issue
Screenshot_86

did u try the script i made perhaps?

I tried of course. It looks very chaotic, no offence. And it doesn’t work

yeah i guess so, was there any errors on the output when u used it?

No there wasn’t, a lot of stuff in the script is just underlined red

screenshot for me pleaseeeeeeee

Screenshot_87
I actually tried to fix it several times, but I couldn’t, there were no underlined red but the script didn’t work anyway

c a p i t a l i z a t i o n lol

the underlined humanoid has a capitalized H and the health on the upperscreen has a lower h

local function Update(health, fine,fine2,caution,danger)
fine.Visible = health >= 80
fine2.Visible = health >= 60 and health < 80
caution.Visible = health >= 30 and health < 60
danger.Visible = health < 30
end

local players = game:GetService(“Players”)

players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)

	local humanoid = character:WaitForChild("Humanoid")
	local hud = player.PlayerGui.Hud:WaitForChild("ImageLabel")
		local fine = hud:WaitForChild("fine")
		local fine2 = hud:WaitForChild("fine2")
		local caution = hud:WaitForChild("caution")
		local danger = hud:WaitForChild("danger")

		Update(humanoid.Health,fine,fine2,caution,danger)

	humanoid.HealthChanged:Connect(function(health)
		Update(health,fine,fine2,caution,danger)
	end)
end)

end)

Frames’re not showing . . . . . . .