How do I make the HP Bar scale correctly?

Basically as the player levels up, the players health and sp will raise and I need the HP Bar to stay the same way it is when the max hp is set to any number. I just need it to always be the same no matter the current and max.

For some reason it over-sizes itself to go past the background bar and sometimes even further. The same thing happens when the max hp is lower then 100 but it under-scales. The Players HP will start lower then 100 and eventually surpass 100 so I need it to always fit in this bar and never exceed it.

I’ve tried multiplying it by the bars length, I’ve tried dividing the health by the max health, I’ve tried parenting the hp bar to a frame. I’m not sure what else to do.

Below is the current Script for it.

local player = game.Players.LocalPlayer
local Character = player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local HpRemaining = Humanoid.Health
local HpMax = Humanoid.MaxHealth
local BarLength = script.Parent.HPMax.HPbar

Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	script.Parent.HPMax.HPbar:TweenSize(UDim2.new(Humanoid.Health / HpMax, 0, 1, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, 0.15, true)
	script.Parent.HP.Text = math.round(Humanoid.Health)
end)

(This is my first forum post so bare with me if somethings off lol)

I think the problem is that you didn’t update the HpMax variable once they leveled up, it still thinks the max HP is 100 even though it should be 150 since you leveled up. The fix is really simple, just move the HPMax variable inside the PropertyChanged function

local player = game.Players.LocalPlayer
local Character = player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local HpRemaining = Humanoid.Health
local BarLength = script.Parent.HPMax.HPbar

Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	local HpMax = Humanoid.MaxHealth
	script.Parent.HPMax.HPbar:TweenSize(UDim2.new(Humanoid.Health / HpMax, 0, 1, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, 0.15, true)
	script.Parent.HP.Text = math.round(Humanoid.Health)
end)
1 Like

Ohhhh I see, Yeah that fixed the problem. Thank you so much!!

1 Like