Healthbar script

so i have a script for my health bar and the script works fine except for the fact that the health appears so small compared to the actual size of the health bar. someone please help me or tell me what i need to do to fix this issue

script:

local player = game.Players.LocalPlayer
local character = player.Character

local healthframe = script.Parent.HealthFrame
local healthbar = healthframe.HealthBar

local staminaframe = script.Parent.StaminaFrame
local staminabar = staminaframe.StaminaBar

while wait() do
	if not character:FindFirstChild("Humanoid") then return end
	
	local health = character.Humanoid.Health
	local maxHealth = character.Humanoid.MaxHealth


	healthbar.Size = UDim2.new(healthframe.Size.X.Scale / maxHealth * health, 0, healthbar.Size.Y.Scale, 0)
end
1 Like

Add a 0 for your first argument for Udim2.new, and use over Offset over Scale. Also, I think you should remove the 0 at the very end since Udim2.new only accept 4 arguments, but I don’t think it would cause a problem anyways.

healthbar.Size = UDim2.new(0, healthframe.Size.X.Offset / maxHealth * health, 0, healthbar.Size.Y.Offset)

1 Like

okay so it just doesnt show the health bar and shows the background instead. how can i change the order where they show? whenever i try to edit the zindex, it never actually works for me

Are you using Scale or offset for your guis? hmm, can you provide a visual of your gui properties.

i use whichever one is on the left side. its the one that changes based on the aspect ratio

Are you sure the bar is behind the background ? The bar might just be so small that you can’t see it. (Because depending on which property you’re using, you might just be multiplying by 0…)
So before you change Scale to Offset, it might be useful to know whether you’re using the offset property, or the scale property for your HealthFrame.

If you’re using Scale, you can use your original code, but you will have to replace “healthframe.Size.X.Scale”, by simply “1” (without quotation marks). Simply because your bar is a direct child of the frame. And an width Scale of 1 means it will take the whole width.

The reason why it appeared so small earlier is because you multiplied by the parent’s scale. Which might be 0.25 for example, so it will appear 4 times smaller than it should.

So basically all you have to do is change that line

healthbar.Size = UDim2.new(healthframe.Size.X.Scale / maxHealth * health, 0, healthbar.Size.Y.Scale, 0)

to

healthbar.Size = UDim2.new(1 / maxHealth * health, 0, healthbar.Size.Y.Scale, 0)

(also, “whichever one is on the left side” is the Scale
it goes like this: XScale, XOffset, YScale, YOffset)

1 Like

ok good cause i thought that the health bar was behind the frame

To further extend the notion of scale with size, see it as a multiplier to whatever size his parent is.
For example

Frame
→ Bar

(Bar is a child of Frame)

If Frame has a width of 200px
And Bar has an XScale of 0.25,
Bar’s width will be 0.25 * 200 = 50px