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

Is this how you use waitforchild()?

Asked by 8 years ago

I'm not so sure of this.

Because i know its like that but what does it do?

local Sound1 = script.Parent.Sound1:WaitForChild()
Sound1:Play()

1 answer

Log in to vote
2
Answered by 8 years ago

No -- you're almost using it correctly, but not quite. Contemplate what that will do for a moment. First the script will look for the script's parent, then it will look for Sound1, and then it will call WaitForChild on Sound1. What if Sound1 doesn't exist? You're pretty sure that's the case since you're using WaitForChild. Since Sound1 doesn't exist, you'd be doing nil:WaitForChild() which would error (you'd also get an error before that from Parent.Sound1 since Sound1 wasn't a child of Parent yet.)

You use it by calling WaitForChild from the parent and passing it the child's name as a parameter. Example usage:

script.Parent:WaitForChild("Sound1")
workspace.Model:WaitForChild("Part")

You call WaitForChild with objects you know for a fact exist. In the previous example, if we didn't know Model would exist, we would do:


workspace:WaitForChild("Model"):WaitForChild("Part")
Ad

Answer this question