I am getting a weird error when using ":WaitForChild()'

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    i want to get the “torso” from the players character as a variable

  2. What is the issue? Include screenshots / videos if possible!
    it keeps giving me this error:

19:31:33.126 - Players.blub20074.PlayerScripts.keep.control:7: attempt to index nil with ‘WaitForChild’

and this is my script:

local players = game:GetService("Players")

local player = players.LocalPlayer

local char = player.Character

local torso = char:WaitForChild("torso")

the script is inside of starterplayerscripts and is a local script
and this is how my explorer looks like:

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    yes i searched on the dev forum and the developer hub, just the same as normal. the weird thing is that i am using :WaitForChild() for a pretty decent time now but i just can’t seem to figure it out.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

I just have no idea

If you had x:Method(), and you get an error "attempt to index nil with Method", this loudly means x is nil.

In your case you’re doing char:WaitForChild("Torso"), you got the error which means when the script ran, char, or the player.Character, was nil. This is because it didn’t load yet. Generally, local scripts that are parented outside of the character will run first, and there is a chance they might run before the character has loaded.

To circumvent this, you need to wait for the character itself to load first. You logically can’t wait for something to load (the Torso) when you’re not sure if its parent (the character) has even loaded. Waiting for the character is a common idiom in Roblox, that you’ll commonly see written this way.

local char = player.Character or player.CharacterAdded:Wait()

What this is saying, if the character, luckily, already loaded, it will just use it without waiting for it to load (in this case it uses player.Character), else if it hasn’t loaded, it will pick wait for it to load using this player.CharacterAdded:Wait(). Explaining what the or is doing, and what even is player.CharacterAdded:Wait() are a rabbit role that I’m sure is gonna confuse you, so let’s not mention it, but I’m happy to explain them if you’re interested.


it worked!!! thank you (still dump of me that i didn’t taught about that :sweat_smile:)

1 Like

make sure to mark it as the solution if it solved your problem for future readers