Is there a difference between ( repeat wait() until FindFirstChild ) and ( WaitForChild )?

I see many programmers use " WaitForChild ", and I see others use " repeat wait() until FindFirstChild "

And I noticed that both do the same job.

" repeat wait() until FindFirstChild " repeats wait until it finds the child.

" WaitForChild " waits for the child to exist

Is there a difference between both?

1 Like

They both do the same thing, however, do not use repeat wait() until FindFirstChild if you care about performance

2 Likes

I mean they both do the same thing, so at the end of the day it’s preference, however, :WaitForChild() is faster as the default time for wait() is around 0.1 - 0.3ms, so it only checks for the object in intervals while :WaitForChild() is more instant. We can check this with a very simple test.

3 scripts, one that creates a part in a folder, and another two that use :WaitForChild and :FindFirstChild and then prints how long it took to detect the part being created.

-- the script that adds the part
Instance.new("Part",script.Parent)
-- the script that uses :WaitForChild() to detect the part being added.
local tickNow = tick()

workspace.Folder:WaitForChild("Part")

print(tick()-tickNow)
-- the script that uses :FindFirstChild() to detect the part being added.
local tickNow = tick()

repeat
	task.wait()
until workspace.Folder:FindFirstChild("Part")

print(tick()-tickNow)

and the results:
image

We can repeat the test a couple of times, and see that in every case :WaitForChild() is faster.

If you use the FindFirstChild method, make sure you’re using task.wait() and not just wait(), in this case it’s faster by around 0.5 seconds.

Overall, they do the same thing, but :WaitForChild is better for performance.

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.