Health gui script wont work as intended

My health GUI script wont work as intended, this is my first time scripting with GUI so im not really supprised, but heres the script

Code
local player = game:GetService("Players").LocalPlayer

local char = player.Character or player.CharacterAdded:Wait()

local hum = char:WaitForChild("Humanoid")

local GUI = script.Parent

hum.HealthChanged:Connect(function(dmg)

GUI.Size = UDim2.new(0, dmg / hum.MaxHealth, 0, 24)

end)

Im not sure why its doing that, help and feedback would be appreciated :smiley:

Switch the GUI.Size to change the Scale of the UI, not the offset.

local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

local GUI = script.Parent

hum.HealthChanged:Connect(function(dmg)
    GUI.Size = UDim2.new(dmg / hum.MaxHealth, 0, 0, 24)
end)

I tried this! thank you1! but

im not sure why this is happening

Code
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum =  char:WaitForChild("Humanoid")

local GUI = script.Parent

hum.HealthChanged:Connect(function(dmg)
	GUI.Size = UDim2.new(dmg / hum.MaxHealth, 244, 0, 30)
end)

I believe you’re changing both the scale and the offset?

i just checked my script and nothing but the scale has changed

Is there a reason the 244 is there in the Offset position? My best guess is this isn’t working as intended due to how your UI is constructed.

ohh i see! ill try to figure something out. This could be the issue

You have 2 problems with this code. You’re only decreasing the bar by the damage dealt but not the current health the player has, which is what you’re going for. You’re also using both an Offset and Scale value which makes the bar resize out of the frame it’s located in.
Change this line:

GUI.Size = UDim2.new(dmg / hum.MaxHealth, 244, 0, 30)

To this line:

GUI.Size = UDim2.new(hum.Health / hum.MaxHealth, 0, 0, 30)
--                       ^                       ^
--              Using humanoid health     No offset value

This worked! thank you!! but theres one small problem

the GUI scales out of the area i’d like it to be. How do i fix that?

Parent the gui inside this frame:

image

Or if the entire thing is an image, you should make a frame that wraps around the healthbar and parent the gui into the newly added frame.