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

what are the differences of FindFirstChild() and WaitForChild()?

Asked by
Grazer022 128
2 months ago

I wanna know the differences since they’re almost similar to me. Thanks!

4 answers

Log in to vote
0
Answered by 2 months ago
Edited 2 months ago

Alright so basically the term FindFirstChild() is like when you want the script to find it right away like so if everything is fully loaded in and you used findfirstchild to a part in workspace it would find it but if you use WaitForChild() the script will wait for what your looking for incase it takes a bit to load in. If you need more help im here.

Ad
Log in to vote
0
Answered by 2 months ago

The difference between :FindFirstChild() and :WaitForChild() are simple when you understand them.

When using :FindFirstChild(), if the child you are trying to find hasn't loaded in it wont be able to find them.

When using :WaitForChild() then function is somewhat alike the wait() function. WaitForChild() will stop the script until it has found the child in its parameters. When it finds the child, it will resume the script.

I hope this was helpful and you now have an understanding of :FindFirstChild() vs :WaitForChild().

Some extra reading I would recommend would be: https://devforum.roblox.com/t/findfirstchild-and-waitforchild-addressing-common-bad-practices-and-habits-of-each/317491

Log in to vote
0
Answered by 2 months ago

:FindFirstChild() finds the first child of the name provided. It can be used to find childs with the same name or find a specific instance. If no child is found, nil is returned.

:WaitForChild() waits for a child with the name provided to be parented under the given parent. It can be used to prevent loading errors.

:WaitForChild() is equal to this function.

local function WaitForChild(parent, childName)
    while wait() do
        for i, child in pairs(parent:GetChildren()) do 
            if child.Name == childName then
                return child
            end
        end
    end
end

Note that the parent paramater isnt required for the built-in function itself.

Log in to vote
0
Answered by 2 months ago

WaitForChild() waits, won't register the target child until the code finds it. I use this for code that runs when the game starts or when something is cloned. That's when parts are loading and the child I'm looking for may not exist yet.

FindFirstChild() doesn't have this feature. If it doesn't find the target instance, even though it's loaded later, it will just pass and return nil.

Answer this question