Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Stupid Question, but how to do I check if something is there?

Asked by
Validark 1580 Snack Break Moderation Voter
10 years ago

There is a specific thing that might be in the workspace that I want to remove at the start of a server.

Will this be true if the part is there?

if game.Workspace.Brick221==true then
--stuff
end

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

No, it won't be true since it would be the Brick!

Actually, if it's not there, it will cause an error.


We use the method FindFirstChild to find children when we don't know if they are there. Instead of erroring, they return nil:

if workspace:FindFirstChild("Brick221") ~= then

Or, since values other than false and nil are truey, we can shorten it to just:

if workspace:FindFirstChild("Brick221") then

Also this is not a stupid question! There's really only this one way to do it (due to this being the only API to do so), so if you didn't know about FindFirstChild you wouldn't have been able to come up with it.

0
I knew about FindFirstChild and have been using it for some time, this was just a question that has always gone unanswered. Thanks Validark 1580 — 10y
Ad

Answer this question