Why is my healthbar increasing in X majorly when damaged

So the issue is whenever I damage my NPC, the healthbar majorly increases in X, although still decreases as how I want it to. I just want the healthbar to stay in place and decrease whenever the enemy receives damage.

This is the issue in a video:

You forgot to attach the script that handles it?

Also, did you identify, whether the health is actually increasing or if it’s just the health bar bugging.

HUM.HealthChanged:Connect(function(Damage)
	HEALTH_BAR1.Size = UDim2.new(Damage / HUM.MaxHealth, 0,0.1,0)
end)

So basically, it divides smaller, Also the script is inside the billboard gui and not the frame.

ow

the healthbar appears to be bugging, as I want it to decrease in X, not increase after 1 remote of damage to the humanoid and then decrease progressively with more damage influenced.

The value passed to the function you connect to the health Changed event is the humanoid’s new health not the damage that was dealt to it. I think you mixed that up…

wait I’m confused heavily, what are you trying to say here? y’know what, I’ll give you the entire script and you can depict where I went wrong and whatnot.

--// IMPORTANT VARIABLES //--

local HEALTH_BAR = script.Parent

--// HUMANIZED VARIABLES //--

local HUM = HEALTH_BAR.Parent.Parent:WaitForChild("Humanoid")

--// VARIABLES //--

local MAIN = HEALTH_BAR:WaitForChild("Main")

--// MAIN'S VARIABLES //--

local NAME = MAIN:WaitForChild("Name")
local LVL = MAIN:WaitForChild("LVL")

local HEALTH_BAR1 = MAIN:WaitForChild("HealthBar")

--// CONFIGURATIONS //-- [IMPORTANT]

NAME.Text = "Placeholder" --The Name
LVL.Text = "LVL: 30" --The Level

HUM.MaxHealth = 1000
HUM.Health = 1000

--// THE HEALTH FUNCTION //-- [IMPORTANT]

HUM.HealthChanged:Connect(function(Damage)
	HEALTH_BAR1.Size = UDim2.new(Damage / HUM.MaxHealth, 0,0.1,0)
end)

The function depicts when the humanoid has received damage, and will connect the gui to decrease for everytime the health has been affected. So when the bar runs out entirely, it displays that the humanoid has been killed.

What I meant to say, is you have the wrong variable name. Anyways, your script looks fine, my theory is when the projectile hits the humanoid, the health Changed function fires, and it is a different size than the default size of the gui. You should check before any projectiles are fired, if the size of that health bar is 1. (which is what it should be because the humanoid starts at max health)

The size of the healthbar is {0.75,0},{0.1,0}. The size of the Y is what is being decreased to deplete the bar’s size. If I changed it to decrease X’s size, it’d break. Yeah it just breaks.

The starting size of the GUI is 0.1. But think about what will happen if that connected function was passed a health of 1000. since the max health is also 1000. The Y of that udim2 will be set to 1000/1000, which is 1, and that’s ten times bigger than the default size of the GUI. That’s why it goes so big.

Ah so if I made the health smaller, the bar would be smaller too? If so, how could I change it so that the bar always stays the same throughout any health set?

If the health bar size should be {0.75,0},{0.1,0} when the NPC’s health is 100%, then just multiply by 0.75 when calculating the size of the health bar.

HUM.HealthChanged:Connect(function(Damage)
	HEALTH_BAR1.Size = UDim2.new(Damage / HUM.MaxHealth * 0.75, 0,0.1,0)
end)

The value you are passing to the Y when setting a new size in that function is like a percentage, so you should have the max size of your GUI as a variable and multiply that value by the max/default size before passing it as the Y.

local defaultSize = 0.75 -- this is the size it should be at max health

HUM.HealthChanged:Connect(function(Damage)
	HEALTH_BAR1.Size = UDim2.new((Damage / HUM.MaxHealth) * defaultSize, 0,0.1,0)
end)

EDIT: if you ever need to change the size of the GUI you can change it in code too

you’re a legend, it works perfectly! Thank you, and thank you atomic as well for trying to help me out! Both help is sincerely appreciated

1 Like

For sure! In the future though, definitely make sure that setting the scale component of the X axis to 1 actually means the bar is at 100% size. Because otherwise it causes confusion lol.

1 Like