Local script breaks when player dies

I have a GUI that shows the players current altitude, it works fine apart from when the player dies, the displayed number is stuck at whatever it was when the player died. here is the script: (local script located within the TextLabel in StarterGui)

local plr=game.Players.LocalPlayer
local label=script.Parent
local head=plr.Character:WaitForChild("Head")
while true do
	local height=head.Position.Y
	local roundheight = math.round(height)
	label.Text=roundheight-4
	wait(0.1)
end

Thanks for any help!

You will need to redefine the character and hence the head every time the player joins. one way you can do this is as such:

local plr=game.Players.LocalPlayer
local label=script.Parent
local head

local function GetHead()
    head = (plr.Character or plr.CharacterAdded:Wait()):WaitForChild("Head")
    head.Destroying:Once(GetHead)
end
GetHead()

while true do
	height=head.Position.Y
	roundheight = math.round(height)
	label.Text=roundheight-4
	wait(0.1)
end

It basically just redefines the head variable whenever the previous head is destroyed

Perhaps you could put this LocalScript into StarterCharacterScripts?

Hmm tried it and had exactly the same problem. Thanks anyway though.

This worked, had no idea StarterCharacterScripts was a thing. Literally exactly what I needed haha. Thanks :+1: