Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does it tell me .Character is "(a nil value)"?

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!

4 comments

0

because the character might not have been loaded when line 3 was ran

0

you cant just have a wait(.01). that means wait for 1/100 of a second. you need to wait at least 1 second before running the script.

0

the best way to tell when it loads is by adding player.CharacterAdded:Wait()

0

local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

4 answers

✓ Accepted answer

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)

1 comment

0

When i use this it doesn't error but also never loads.

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

This discussion is preserved as it was. New answers, comments, and votes are closed.