Problems scaling a health bar

Hello guys!
Today I was doing a custom health bar, but when I play test it looks like this:

The script:

local textLabel = script.Parent.TextLabel
local canvas = script.Parent.CanvasGroup
local textHealth = canvas.textHealth
local background = canvas.Background
local healthbar = background.Health

local gui = script.Parent

task.wait()

local mainHealthGui = script.Parent

local char = mainHealthGui.Parent.Parent
local human: Humanoid = char:WaitForChild("Humanoid")

local plr = game.Players:GetPlayerFromCharacter(char)

local mh = human.MaxHealth

textLabel.Text = plr.DisplayName.." (@"..char.Name..")"

healthbar:TweenSize(UDim2.new(human.Health/mh, 0, healthbar.Size.Y.Scale, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.1, false)

textHealth.Text = human.Health .. "/" .. human.MaxHealth

human.HealthChanged:Connect(function(currentHealth)

	mh = human.MaxHealth

	healthbar:TweenSize(UDim2.new(currentHealth/mh, 0, healthbar.Size.Y.Scale, 0),  Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.1, false)

	textHealth.Text = math.round(currentHealth) .. "/" .. mh

end)

I tried fixing the bug, but nothing worked.

set AlwaysOnTop to true
char limit

it still not working after that…

Can you perhaps send or DM me a rbxl with just the healthbar ui and the script? I will happily take a look into it myself.

sure, I just need to know how to send a dm

Click on my avatar then message.

Are you able to check what the scale is on the ui when you load in? not only does your code look fine to me, I can’t see anything wrong when I try it.

when I join (without select chocolate guy) it stays on {1, 0},{1 ,0}, when I select him it goes to {0.667, 0},{1, 0}.

I just saw that if I manually change my max health it works normally, but when a script changes it, it bugs totally.

Toggle AlwaysOnTop for mainHealthGui.

Y’ALL, I FOUND A SOLUTION!

I just changed the part:

human.HealthChanged:Connect(function(currentHealth)

	mh = human.MaxHealth

	healthbar:TweenSize(UDim2.new(currentHealth/mh, 0, healthbar.Size.Y.Scale, 0),  Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.1, false)

	textHealth.Text = math.round(currentHealth) .. "/" .. mh

end)

to:

while task.wait() do
	healthbar:TweenSize(UDim2.new(human.Health/human.MaxHealth, 0, healthbar.Size.Y.Scale, 0),  Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.1, false)
	textHealth.Text = math.round(human.Health) .. "/" .. human.MaxHealth
end

I didn’t understand why this way works, but its now fixed!