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

using alot of waitforchilds better way?

Asked by 4 years ago

hello i find myself using alot of waitforchild's is there a better way to do this?

Example

workspace:WaitForChild("model"):WaitForChild("brick"):WaitForChild("ClickDetector")

it seems super inefficient to me any better way of doing this?

0
I mean, if you want to make sure they all exists or wait until they exist then "WaitForChild" is probably the easiest and the most effective way 0msh 333 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago

Try this:

repeat wait(0.01) until workspace:FindFirstChild("model")
repeat wait(0.01) until workspace.model:FindFirstChild("brick")
repeat wait(0.01) until workspace.model.brick:FindFirstChild("ClickDetector")
0
Less efficient than what the OP had (see https://en.wikipedia.org/wiki/Busy_waiting) hiimgoodpack 2009 — 4y
Ad
Log in to vote
1
Answered by 4 years ago

Just gonna leave this here in-case someone else stumbles upon it and is confused since the accepted answer is actually less efficient. Using the WaitForChild() function is completely fine and is the best possible way to wait for an instance. However, there are ways you can write it so that there are less of the function.

For example, say we had a Model class named "X" in Workspace. Inside of X, we also have A and B, which we need for our code. Instead of looking for them individually, we can look for "X" first, and then locate A and B from X's index. It would look something like this:

--// Variables //--

local X = workspace:WaitForChild("X");

local A,B = X:WaitForChild("A"), X:WaitForChild("B");

Answer this question