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

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

Asked by 5 years ago

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!

0
it yields the current thread until the child is found theking48989987 2147 — 5y
0
if you just want to see if something exists, use FindFirstChild theking48989987 2147 — 5y
0
pauses the script greatneil80 2647 — 5y
0
until item found greatneil80 2647 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

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
0
I suppose I didn't understand the definitions of 'yield' and 'thread' in scripting terms. Thanks! Likely_Lad 4 — 5y
Ad

Answer this question