Basically this script should continuously update the GUI of the Humanoids Health but instead it errors, I really can't figure out why...
Code:
wait(0.1) while true do local Humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid") local hp = Humanoid.Health/100 script.Parent:TweenSize(UDim2.new(hp,0,1,0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,0.15) wait(.2) end
Error Given:
Players.PastDays.Backpack.HoneyBadger.clientMain.mainGUI.HP.Frame.HealthDisplay:3: attempt to index field 'Character' (a nil value)
Any help appreciated!
Please accept It is most likely that the users character has not loaded. To fix this problem, you can at the top add
This is probably the best way
repeat wait() until game.Players.LocalPlayer.Character
which will not let the script run until the player's character has been added. Another way to do this is by using a function:
function runLoop() while true do local Humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid") local hp = Humanoid.Health/100 script.Parent:TweenSize(UDim2.new(hp,0,1,0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,0.15) wait(.2) end end game.Players.PlayerAdded:Connect(function(plr) plr.CharacterAdded:Connect(function(chr) runLoop() end) end)
wait(1) while true do local Humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid") local hp = Humanoid.Health/100 script.Parent.Text = "Health: "..Humanoid.Health.."/100" script.Parent:TweenSize(UDim2.new(hp,0,1,0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,0.15) wait(.2) end
Try adding in a variable for Character so that the character can load before the loop runs.
local Character = game.Players.LocalPlayer:CharacterAdded.Wait() wait(0.1) while true do local Humanoid = Character:WaitForChild("Humanoid") local hp = Humanoid.Health/100 script.Parent:TweenSize(UDim2.new(hp,0,1,0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,0.15) wait(.2) end
Just wait until the character add.
And put it in while loop
Easy
while true do repeat wait() until game.Players.LocalPlayer.Character ~= nil local Humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid") local hp = Humanoid.Health/100 script.Parent:TweenSize(UDim2.new(hp,0,1,0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,0.15) wait(.2) end