How can I make the health bar resize depending on the humanoid maxhealth

hey,
I’m trying to make a health which resize depending on the player max health. because when the player grab a bullet proof vest, the health bar go outside the box. here is the code :

local health = game.ReplicatedStorage.health

local player = game.Players.LocalPlayer

local maxHealth = player.Character:WaitForChild("Humanoid").MaxHealth

local Players = game:GetService("Players")

local Teams = game:GetService("Teams")

local humanoid = player.Character:WaitForChild("Humanoid")

health.Changed:Connect(function()

local h = player.Character:WaitForChild("Humanoid").MaxHealth

script.Parent.Size = UDim2.new( health.Value / player.Character:WaitForChild("Humanoid").MaxHealth,0,1,0)

script.Parent.Parent.HealthDisplay.Text = 'Health : '..health.Value

end)

The scale value on the X Axis of the HealthBar would need to be multiplied by its default width so that when it ends up reaching something such as 50%, it would be 50% of the space that you allocated for it and not 50% of 1.

Example:

local percent = Humanoid.Health / Humanoid.MaxHealth * 0.794 -- Number to replace

--[[

0.794 represents the size of the HealthBar's X Axis (in scale) when at MaxHealth

This means that a scale of 0.397 will have exactly half of the Healthbar drained
instead of 0.5 where it'd be around 62-63% when considering the default scale

--]]

script.Parent.Size = UDim2.new(percent, 0, HealthBar.Size.Y.Scale, 0)

scale is the % of the size of its parent
From what you said, you are guessing that the parent of the bar is the gui itself and not any resized frame or something. Just saying

The OP didn’t provide enough information about the hierarchy of the GUI so I had to guestimate the most probable cause of the issue described. Aside from that, from my observations, there’d be no reason for the HealthBar to exceed its bounds (unless the “health” that’s stored in the ReplicatedStorage happened to be above the Humanoid’s MaxHealth).

I changed the way of the display, it was based on the player humanoid health value and maxhealth value

Can’t you just use the Humanoid.HealthChanged event instead to make it easier…?