Health bar not working after reset

Good evening, I have a health bar I am trying to make which works fine and only stops working after I reset my character. Heres my code below:

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild('Humanoid')

local frame = script.Parent
local HBar = frame.bar
local scale = HBar.Size.X.Scale

function update()
	local hp = hum.Health/100
	HBar:TweenSize(UDim2.new(hp * scale, 0, 0.48, 0),"Out","Sine",0.5)
	print("Updated health")
end

update()
hum.HealthChanged:Connect(update)

Does anyone know how to fix this issue?

You need to re-identify the player’s character every time they die. After they die, their old character does not exist, so any variables referencing the old character won’t work unless they are updated with the new character.

1 Like

You can put the script in StarterCharacterScripts and re-reference the guis or set the ResetOnSpawn property to true

1 Like