Detecting if a WaitForChild() or FindFirstChild() is nil?

Normally, a WaitForChild() or FindFirstChild() is nil they return an error. But is there a way to detect when they return nil without prompting an error?

Put it in a pcall. (that might help)


local thing = workspace:FindFirstChild"Thing"

if not thing then
print"Nonexistent"
end

They don’t error. :WaitForChild will just infinite yield unless you specify a second argument of how many seconds to yield for.

You easily then check both of these if they’re nil
with an if statement if you’re not doing an infinite yield WaitForChild.

if workspace:FindFirstChild("YourObject") == nil then
  print("nil")

WaitForChild would just give infinite yield possible.

1 Like

Assign it to a variable and then execute it by checking if the variable is nil. The only time when it errors is only when you try to run a function(or write) on it when it’s nil.

1 Like