HealthBar Size Issue

Hello, my healthbar script works, but the problem is, when the health changes, the healthbar will size very big.

I’ve tried many solutions, nothing worked.

Explorer:
Screenshot_299

Property Explorer:

Screenshot_300

Code:

local plr = game.Players.LocalPlayer
local chr = plr.Character or plr.CharacterAdded:Wait()
local humanoid = chr:WaitForChild("Humanoid")
local formula = math.clamp(humanoid.Health/humanoid.MaxHealth, 0, 1)

script.Parent.Parent:WaitForChild("Percentage").Text = "Health: " .. humanoid.Health .. "%"

local function update()
	
	script.Parent.Parent:FindFirstChild("Percentage").Text = "Health: " .. humanoid.Health .. "%"
	script.Parent:TweenSize(UDim2.fromScale(formula, 1), "InOut", "Linear", .5, false)
end

humanoid.HealthChanged:Connect(update)
humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(update)

Video:

I think this is what’s causing your issues. Try dividing this by 10.

What’s the bar’s default size value?

I’m using math.clamp and dividing the current health by the max health. (e.g: my health is 100, max health is 100. 100/100 = 1). And I am using math.clamp with the minimum being 0, maximum being 1, keeping the bar from sizing or breaking.

Try to put humanoid.Health/humanoid.MaxHealth directly into where you put formula and it should work.

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

script.Parent.Parent:WaitForChild("Percentage").Text = "Health: " .. humanoid.Health .. "%"

local function update()
	
	script.Parent.Parent:FindFirstChild("Percentage").Text = "Health: " .. humanoid.Health .. "%"
	script.Parent:TweenSize(UDim2.fromScale(humanoid.Health / humanoid.MaxHealth, 1), "InOut", "Linear", .5, false)
end

humanoid.HealthChanged:Connect(update)
humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(update)

Just tried that right now, it came with the same result, and was still oversizing.

Screenshot_301
Screenshot_302

Another way to accomplish this is like this:

gui:TweenSize(humanoid.Health/humanoid.MaxHealth, "InOut", "Linear", .5, false)

This video explains:

Quite interesting. Also btw…

your local formula = math.clamp(humanoid.Health/humanoid.MaxHealth, 0, 1)
is outside your function which means that it isn’t updating in the update function.

Not the fix to the problem, but definitely a fix to address

local plr = game.Players.LocalPlayer
local chr = plr.Character or plr.CharacterAdded:Wait()
local humanoid = chr:WaitForChild("Humanoid")
--/!!/  local formula = math.clamp(humanoid.Health/humanoid.MaxHealth, 0, 1)

script.Parent.Parent:WaitForChild("Percentage").Text = "Health: " .. humanoid.Health .. "%"

local function update()

local formula = math.clamp(humanoid.Health/humanoid.MaxHealth, 0, 1)
	
	script.Parent.Parent:FindFirstChild("Percentage").Text = "Health: " .. humanoid.Health .. "%"
	script.Parent:TweenSize(UDim2.fromScale(formula, 1), "InOut", "Linear", .5, false)
end

humanoid.HealthChanged:Connect(update)
humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(update)

I attempted the exact solution.
Still comes back as this:

local plr = game.Players.LocalPlayer
local chr = plr.Character or plr.CharacterAdded:Wait()
local humanoid = chr:WaitForChild("Humanoid")
local formula = math.clamp(humanoid.Health/humanoid.MaxHealth, 0, 1)

script.Parent.Parent:WaitForChild("Percentage").Text = "Health: " .. humanoid.Health .. "%"

local function update()
	
	script.Parent.Parent:FindFirstChild("Percentage").Text = "Health: " .. humanoid.Health .. "%"
	script.Parent:TweenSize(UDim2.new(formula, 0, 1, 0), "InOut", "Linear", .5, false)
end

humanoid.HealthChanged:Connect(update)
humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(update)

Tried that, the same thing.

Not the fix to the problem. However, it was an issue to address sooner or later.

Otherwise humanoid.Health/humanoid.MaxHealth will never be updated or changed to what humanoid.Health/humanoid.MaxHealth is if it’s not run in the function is what I meant.

Yep, I tried that.

Just to go in-depth in the situation, I made a seperate roblox place for investigation.
problem.rbxl (35.0 KB)

The issue is interesting!
I’m not quite sure. The structure of the code besides that looks good to go as you and I would assume.

Could be something with the properties but I haven’t got familiar enough with them to say anything.

Okay so, I figured everything out using the temp you gave.
Here is the working script. and I’ll explain below.

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

script.Parent.Parent:WaitForChild("Percentage").Text = "Health: " .. humanoid.Health .. "%"

local sizeX = script.Parent.Size.X.Scale --/!!/  
local sizeY = script.Parent.Size.Y.Scale --/!!/  

local function update()
	local formula = math.clamp(humanoid.Health/humanoid.MaxHealth, 0, 1)

	local finalX = sizeX * (formula) --/!!/  
	
	script.Parent.Parent:FindFirstChild("Percentage").Text = "Health: " .. humanoid.Health .. "%"
	script.Parent:TweenSize(UDim2.fromScale(finalX , sizeY), "InOut", "Linear", .5, false)
end

humanoid.HealthChanged:Connect(update)
humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(update)

I may be awful at explaining this, as it is late while I’m typing this.

The reason it wasn’t working was due to using the X / Y scale of a GUI object is the % of what the scale of the screen is. For example what we were seeing is if you had 90% health, the tween is going to blow up the GUI object to 90% of the current X.Scale value of the screen.

So as soon I figured that out I just quickly got the X & Y scale themselves and took that and multiplied it by the % of health using you’re formula variable.

Side Note::
The sizeX & sizeY variables stay out of the function so they aren’t suppose to be updated every time the function is ran, otherwise you end up with undesired results if it calculates a new sizeX & Y every time updated is ran.


Edit: Spelling Errors

1 Like
local formula = math.clamp(humanoid.Health/humanoid.MaxHealth, 0, 1)

This is just getting your current humanoid’s Health and MaxHealth properties when the script first executes and assigning the value returned by math.clamp(), if you want this ‘formula’ to be used dynamically then wrap it inside a function.

2 Likes

first, instead of using formula, do it inside of the the function.

Try this:

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

script.Parent.Parent:WaitForChild("Percentage").Text = "Health: " .. humanoid.Health .. "%"

local function update()
	
	script.Parent.Parent:FindFirstChild("Percentage").Text = "Health: " .. humanoid.Health .. "%"
	script.Parent:TweenSize(UDim2.new(math.clamp(humanoid.Health/humanoid.MaxHealth, 0, 100), 0, 1, 0), "InOut", "Linear", .5, false)
end

humanoid.HealthChanged:Connect(update)
humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(update)

Instead of declaring a variable, and using it, expecting it to change when you use it, just do it inside of the update() function.

When I made a health bar gui, I used this exact method.

Apparently, this does work, but it still sizes down (figured it was the X scale)

Yep! It works, I understand this. (figured it was the X axis).

Thanks!