How to find a child with a specific name

So I want to find a specific part with a specific name and idk know how to look inside a model and find a specific part with a different name then the other parts using a script.

2 Likes

You use FindFirstChild.

Take a look at this link:

Try using this code (example) :

local MyModel = game.Workspace.MyModel
local MySpecificPart = MyModel:FindFirstChild("MySpecificPart")
5 Likes

If you know it exists, you can also do this:

local MyModel = game.Workspace.MyModel
local MySpecificPart = MyModel["MySpecificPart"]

Sometimes I find this to be easier to read–however, if the child doesn’t exist then it will error.

3 Likes

Wouldn’t we also be able to do this as well?

If we know it will exist or do know it exists

local MyModel = game.Workspace.MyModel
local MySpecificPart = MyModel:WaitForChild("MySpecificPart")
2 Likes

Yea, in total:

  • MyModel:FindFirstChild("SpecificPart") works to find out if the part exists right now. If the part doesn’t exist/isn’t found, it will return nil.
  • MyModel:WaitForChild("SpecificPart") works for if the part exists or will probably exist soon. It returns the part if it exists, and if not, it will wait for the part to exist and then return it. This works best for when the client is loading and you don’t know if the client will have loaded everything yet. You can add a number timer, such as MyModel:WaitForChild("SpecificPart", 5), to tell the code to wait no longer than x seconds for the part to exist. If it doesn’t exist in that time, it will return nil and the code will continue.
  • MyModel["SpecificPart"] works if you know the part currently exists. It’s not safe and will error if the part doesn’t exist (so use FindFirstChild), but I sometimes use this because FindFirstChild looks bulky
3 Likes