Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

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

Asked by 4 months 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 1808 — 4mo
0
if you just want to see if something exists, use FindFirstChild theking48989987 1808 — 4mo
0
pauses the script greatneil80 1140 — 4mo
0
until item found greatneil80 1140 — 4mo

1 answer

Log in to vote
0
Answered by 4 months 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 — 4mo
Ad

Answer this question