Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Clarification: does WaitForChild pause a script or just delay the variable being assigned?

I've been having problems with WaitForChild in a couple of my scripts, which makes me think maybe I've misunderstood how it works. On the wiki it says "If the child does not exist, it will yield the current thread until it does."

Does this mean it pauses the entire script on that line until the object mentioned is loaded, or simply that the variable won't be assigned until the object is loaded?

I've been using it in the former context, trying to make my whole script wait until other scripts have loaded (to make sure required global variables are defined) but it hasn't been working.

Just wanna check this point before I troubleshoot any further.

Thanks!

4 comments

0

it yields the current thread until the child is found

0

if you just want to see if something exists, use FindFirstChild

0

pauses the script

0

until item found

1 answer

✓ Accepted answer

You said it yourself - it will yield the current thread (in your case, the entire script unless you're using spawn or coroutines) until the child exists. You can test this pretty easily with some code in the command line:

game:WaitForChild("hey I don't exist")
print("g") -- this should never print

1 comment

0

I suppose I didn't understand the definitions of 'yield' and 'thread' in scripting terms. Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.