How do I fix my health bar and how does it work?

I’m trying to make a health bar. I didn’t know how, so I quickly went and searched a Youtube video. I found a video, but it just gave me the script with no explanation. I’m now experiencing an issue where this happens when the player is damaged and gets their full health back:
image

(This is how it’s supposed to look btw)
image

Here is my script:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

humanoid.HealthChanged:Connect(function(Damage)
     script.Parent.Size = UDim2.new(Damage / humanoid.MaxHealth, 0, 0.8, 0)
end)

I don’t know how to fix this. If you do know a solution, can you care to explain to me how it works too?

No idea why thats happening, has happened to me before but the correct script should be:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

humanoid.HealthChanged:Connect(function(Damage)
     script.Parent.Size = UDim2.new(humanoid.Health / humanoid.MaxHealth, 0, 0.8, 0)
end)
1 Like

I have found a fix for my situation!
I set my healthbar’s parent as another frame.
image

It basically kept changing its size to that parent.

I have also decided to change my healthbar to this, and it works.
image