Help checking if something is in workspace with script

So I’m still beginner at scripting and I was wondering if there was a way to check if something is in the workspace because many things are going in and out of ServerStorage and Workspace so pretty much I want to check if a certain part is in the workspace or not and make an outcome of true or false. I tried doing this:

game.workspace:FindFirstChild("Mexico")

but if Mexico isn’t in the Workspace an error occurs. Any information would be helpful.

local Mexico = game.Workspace.Mexico

If Mexico == nil then
— do something
end

Sorry I can’t format I’m on mobile, but this would do the if statement if Mexico doesn’t exist

You have to put it in an if statement

if game.Workspace:FindFirstChild(“Part”) then
—part is there
else
— part is not there
end

Not a good idea as it will error when the instance Mexico does not exist.


@FlyingGorillaFanNoob your attempted solution is the way to go.
Instance:FindFirstChild returns nil when a child instance is not found, while indexing using the dot operator throws when an instance is not found.

local found = workspace:FindFirstChild("Mexico")
if (found) then 
    print("found")
else 
    print("not found") 
end 

Note that you can use the workspace builtin interchangeably with game.Workspace.

1 Like