Health Bar gets too big when health goes up

When I get a health boost in my game the health bar size gets too big. How do I fix this?
image
image

You can use math.min for this. Something along the lines of

barSizeX = math.min(barSizeX, 1)

You can also use math.clamp for this, it returns a value with a min/max limit.

barSizeX = math.clamp(Humanoid.Health, Minimum Value, Maximum Value)
1 Like

I tried that and it didn’t work. This is my script

wait(.1)
local Player = game.Players.LocalPlayer
local Character = Player.Character

while true do
	hp = Player.Character:WaitForChild("Humanoid").Health/100
	script.Parent:TweenSize(UDim2.new(hp,0,1,0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,0.15)
    barSizeX = math.min(barSizeX, 1)
	wait(.2)
end

Little late reply, sorry about that. So in this case you would change barSizeX to hp. This code should work.

wait(.1)
local Player = game.Players.LocalPlayer
local Character = Player.Character

while true do
	hp = math.min(Player.Character:WaitForChild("Humanoid").Health/100, 1)
	script.Parent:TweenSize(UDim2.new(hp,0,1,0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,0.15)
	wait(.2)
end
2 Likes

Thanks you so much, this one works.