I don't know what is wrong with this GUI?
char = script.Parent.Parent.Parent.Parent.Character hum = char.Humanoid hum.Changed:connect(function() script.Parent.HealthBar.Size = UDim2.new(hum.Health/hum.MaxHealth,0,1,0) end)
First off, if you are dealing with a GUI and performing a task as simple as this, you should use a LocalScript to help reference the player with much more easily.
Using a local-script allows you to use 'LocalPlayer', a simple shortcut to finding the Player on the client.
local player = game.Players.LocalPlayer print(player.Name) --Prints the players name
Also, be careful, the players character may not always have spawned as local-scripts load faster than most things in a game.
To make sure the character is ready, we can do a :wait()
on the CharacterAdded
event if the character hasn't been found:
local player = game.Players.LocalPlayer local char = player.Character or player.CharacterAdded:wait() print(char.Name) --Will print the Characters Name
Now, let's combine all that and fix your script:
local player = game.Players.LocalPlayer local char = player.Character or player.CharacterAdded:wait() local hum = char:FindFirstChild("Humanoid") hum.HealthChanged:connect(function(health) script.Parent.HealthBar.Size = UDim2.new(health/hum.MaxHealth,0,1,0) end) --HealthChanged is a special event fired only when the Health is Changed
char = script.Parent.Parent.Parent.Parent.Character hum = char.Humanoid hum.Health.Changed:connect(function() script.Parent.HealthBar.Size = UDim2.new(hum.Health/hum.MaxHealth,0,1,0) end)