Error while using WaitForChild()

I wrote a script, but it’s giving me this error:

Error

Can someone please explain to me what this error is and what it means? I would really appreciate it!

It means you’re trying to use :WaitForChild() on a nil value somewhere in your code.

1 Like

But, I’m not using it on a nil instance, I’m using it to wait for the humanoid root part of a character.

Error(1)

Can you show us the part where you declare the LocalPlayerCharacter variable? You’re probably trying to declare the LocalPlayerCharacter before the game even adds the character to workspace.

https://gyazo.com/13bf4e9962de4ff17cf90baf1883879f

https://gyazo.com/52809033ef67ae6a2e630c51da1e51aa

That probably means that the Character does not exist yet.

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait() -- waits for the character to load if it doesn't exist
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

LocalPlayer.CharacterAdded:Connect() will not yield your code to wait for the character, you should use local LocalPlayerCharacter = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait() instead.

2 Likes

Oh I just do:

local player = game:GetService("Players").LocalPlayer
local character = player.Character
local HRP = player.Character.HumanoidRootPart

Can you show me the location of the local script?

I tried it and it worked! Thank you so much for your help!