Why wont the gui work?

I have this health GUI made by one of my friends, and i scripted it so that its like a health bar but it wont work and im not sure why.

heres the script

Code
local dummy = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent
local hum =  dummy:WaitForChild("Humanoid")

local GUI = script.Parent


local endPos = UDim2.new(hum.Health / hum.MaxHealth, 0, 0, 19)

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

help and feedback is appreciated :smiley:

Would there happen to be any errors on your Output at all? That would help out a lot (You can find it on the top within View > Output)

1 Like

there are no errors in the output. i told the script to print something but nothing was printing either :sweat_smile:

1 Like

Are you damaging the dummy through a server script or a local script?

Could be issue be happening somewhere else then? Try adding a print after you define your variables

print("WE KNOW THIS WORKS SURELY")

local dummy = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent
local hum =  dummy:WaitForChild("Humanoid")

local GUI = script.Parent

print("Maybe there isn't a Humanoid in the 7 Parents you referenced?")

local endPos = UDim2.new(hum.Health / hum.MaxHealth, 0, 0, 19)

hum.HealthChanged:Connect(function(dmg)
    print("Detected change")

	GUI.Size = UDim2.new(hum.Health / hum.MaxHealth, 220, 0, 17)
end)
1 Like

Is it a LocalScript? If so, the code’s not running at all because LocalScripts don’t run while parented under any descendant of the Workspace.

1 Like

The issue was most likely caused from the 220 offset in the UDim2, combining with the scale messing the entire thing. Also .HealthChanged returns the current health not damage:

--above code remains the same
local endPos = UDim2.new(hum.Health/hum.MaxHealth, 0, 0, 17)
hum.HealthChanged:Connect(function(health)
	GUI.Size = UDim2.new(health/hum.MaxHealth, 0, 0, 17)
end)
--also if 220 was an attempt to scale the bar, just do (health/hum.MaxHealth)*multiplier in scale. With multiplier being the aspect ratio(for example 5)

LocalScripts don’t work inside the workspace ( Lots of .Parents for the dummy model btw, I wonder what you put inside that dummy lol)

i found the solution, after looking through everyones comments i realized that the script should be in a local script, and that the offset value has to be 0.