I'm having problems with my healthbar GUI

I can’t really explain this problem, so here are images describing the problem.

This is what the bar looks like before I test the game:

And this is what happens when I test the game:

Here is the code, so you can figure out what is wrong here.

local bar = script.Parent.Bar

local healthBar = script.Parent.Health

local plr = game.Players.LocalPlayer

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

local colorRed = Color3.fromRGB(255, 0, 25)
local colorDefault = Color3.fromRGB(146, 237, 255)

while wait() do
	if not char:FindFirstChild("Humanoid") then return end
	
	local health = char.Humanoid.Health
	
	local maxHealth = char.Humanoid.MaxHealth
	
	if char.Humanoid.Health <= 25 then
		healthBar.BackgroundColor3 = colorRed
	end
	if char.Humanoid.Health > 25 then
		healthBar.BackgroundColor3 = colorDefault
	end
	healthBar.Size = UDim2.new(bar.Size.X.Scale / maxHealth * health, 0, healthBar.Size.Y.Scale, 0)	
end

I hope you can figure out what’s causing this.

1 Like

idk if I’m answering this correctly, but you might have to expand the outline

I can’t, the gui uses a CornerUI thing.

I was just scanning through the code. You would have to change this @DevForum_Acc. At least that’s what I think. Is there any errors in the output?

Only use one of the bars to get the new size. I think the issue here is that one bar is a little smaller and that messes up the calculation since you mix them in the size assigment.

Basically, in this assigment - healthBar.Size = UDim2.new(bar.Size.X.Scale / maxHealth * health, ...), you are trying to set the size of one of the bars, but you use a different bar’s size to calculate the size. So instead do - bar.Size = UDim2.new(bar.Size.X.Scale / maxHealth * health, ...).

But I HIGHLY recommend just putting the transparent outline bar inside the opaque bar and setting the size to like 1.1, 0, 1.1, 0 so it’s a little bigger. It will then resize automatically and you dont have reference two separate bars.

Also, you should absolutely not use a while loop for detecting a health change. Use humanoid.HealthChanged:Connect(function(health) ... end) instead.

Also @DevForum_Acc the health bar might be a bit off because you just dragged it into position and didn’t scale the x, y and z.

That’s not the problem, since he’s using the same resolution to design and test the game. Neither do ui elements have a z axis.