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

using alot of waitforchilds better way?

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?

1 comment

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

2 answers

✓ Accepted answer

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")

1 comment

0

Less efficient than what the OP had (see https://en.wikipedia.org/wiki/Busy_waiting)

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");

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