Script "attempt to index a nil value" When Searching For an Existing Child In Player Model

Hello, thanks for checking out my post. I have struggled with this for a while now and have searched up how to fix and how to do but still cannot fix my error, and i’m sure i’m making a simple mistake here.

I am trying to destroy the player’s shirts and pants right when they join the game. (I’m making a retro game so I am using body colors instead of shirts or pants).

This is the full script that I mention in this post:

In my script, I have code searching for the first children named “Shirt” and “Pants”, and after that I have the function Destroy() to remove those children from the player’s character workspace model.

For some reason, I am getting this error:

It says “attempt to index a nil value” after it searches for the player’s shirt, which is the line of code before searching for the player’s pants.

As you can see, when I join and am fully in game, inside my character model, “SirRaviolli”, I have the children “Shirt” and “Pants”:
helpme_explorer

(I did fully search for if I spelled incorrectly anything and for any mistakes I usually make in my code and I have checked that when it checks for the new player’s name it is not returning nil and that the workspace character model does exist and is found.)

I have been trying to fix this for some minutes now after searching for ways to fix this and trying different ways to find the child “Shirt” and “Pants”. I have tried directly going to the player’s legs and torso parts, and also tried using :FindFirstChild instead.

I must be making a small mistake that I am just not aware of, please leave some help below thanks :slight_smile:

Use WaitForChild instead. You’re experiencing an edge case where your code runs before the contents of the character are fully added. A note that your code will not clear characters upon respawning, so you could also include a CharacterAdded function.

2 Likes

You’re right I didn’t think of that, let me try it out thanks :slightly_smiling_face:

Edit: It works! I always forget these simple little things, thank you for the help!