The difference between WaitForChild and FindFirstChild

Can someone please tell me the difference between WaitForChild and FindFirstChild. I am a beginner, so…

I was just making a teleporting part when I was using WaitForChild and wanted to know the difference…

workspace:WaitForChild(“scriptpart”)

workspace.scriptpart.Touched:Connect(function()
workspace.scriptpart.CFrame = CFrame.new(Vector3.new(15.215, 0.5, -19.005))

end)

3 Likes

The :FindFirstChild() function returns nil if an instance with the name specified is not found (has a slightly larger overhead than direct indexing), :WaitForChild() waits for an instance (yielding the current thread until it does), if it is not found in under 5 second then an ‘infinite yield’ error is thrown, it’s useful when you aren’t sure whether an instance has replicated yet.


This could’ve been answered easily through some research

https://developer.roblox.com/en-us/api-reference/function/Instance/FindFirstChild
https://developer.roblox.com/en-us/api-reference/function/Instance/WaitForChild

Edit: added example

 -- accessing an object that exists implicitly under ReplicatedStorage 
 game:GetService("ReplicatedStorage").Event -- perfectly fine

 -- accessing an object that you're not sure has replicated to a client
 local part = workspace:WaitForChild("Part", 5) -- return nil if not found

3 Likes

WaitForChild() Waits unlimited times and doesn’t carry on the script until loaded

FindFirstChild() Checks once and can carry on without sometimes causing errors.

(SIMPLISTIC)

9 Likes

Ok, thanks. I kind of understand now with all these high defined words.

1 Like

OH, OK THANK YOUUUUUU. Now I understand and I’ll use WaitForchild when I want something to wait when loading in.

1 Like

WaitForChild() Waits unlimited times

:WaitForChild() stops an infinite/unlimited yield from happening, hence the “infinite yield possible” message. It also has a second parameter which is a timeout, basically how much time to wait for the object to load, if the object didn’t load by the time the timeout is over, it will just return nil.

obj:WaitForChild("Part", 5) --gives Part 5 second to load else it carries on without it, yields for 5 seconds
3 Likes

As a small addition to WaitForChild, you can also use :Wait() for the players characteradded event (example below)

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

I’ve heard that there are also other examples of this but I have yet to find them

1 Like

I don’t think that’s correct.

If WaitForChild thinks the object doesn’t exist, it stops, raising an Infinite yield possible warning in console.

In addition, both will return nil if the object is not found.

In answer to the poster’s question, findFirstChild is useful to check if an object exists. WaitForChild is more useful when you are sure an object exists, but need to wait until it has replicated.

You can see this difference in the function design. WaitForChild warns you if the object is not found, findFirstChild just returns nil.

3 Likes

I know this is pretty much unrelated to the OP, but :Wait() just waits for an event and then returns the arguments that event would pass. You can read about it here

3 Likes

In brief terms, WaitForChild(Instance, TimeThreshold) should be used if for certain that something exists, and should probably be used in local scripts when indexing anything. FindFirstChild(Instance) is basically to check if something exists.


Let’s put it to some use.

--// Local Script
local Leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats")
--[[ 
    WaitForChild is used here because
    leaderstats might not have loaded 
    in yet
--]]
--// Server or Local Script
workspace.Part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        -- Code
    end
end)
--[[
    FindFirstChild is used here because
    we are trying to find out if the parts
    parent has a humanoid child
--]]

WaitForChild() should be used to getting instances that need time to load in and that you are 100% sure that it exists. To check if an object exists, you should only utilize FindFirstChild()

3 Likes

I’d just like to elaborate on the examples slightly, because they are excellent ones.

Ok, here you can see that we are in a localscript. The comment below explains that leaderstats may not have loaded yet. By this, @VegetationBush means that the object was created on the server. In order to keep clients in sync, Roblox’s server replicates objects to the client. However, your code needs to interact with these. Attempting to operate on an object which has not loaded will result in the Attempt to index a nil value error we all kind of hate.

To fix this, we need to wait for the thing to be confirmed as loaded. Luckily, Roblox gives us this function.

A note on it, though. It should be used sparingly. The performance impact of this is slightly higher than just indexing normally. In addition, doing this in code which needs to run fast is bad because of the yield.

In essence, in order to keep your performance high, use this function once and keep a reference to the returned result. This ensures that the object was replicated. So, for example, use the function to get the leaderstats in a variable and then use that variable from then on. It’s not good practice to continually WaitForChild for the exact same instance. It’s a waste of performance, don’t do it.

In this example, the hit’s parent could be any instance. It could be workspace, it could be a generic model or even some random part. So, we can’t assume the object is there. This function allows us to check this.

In Lua, if nil is passed as the value to an if statement, it will be treated as though it were false. So, basically, if there is no humanoid, we get false back.

This allows us to discard hits from non-players.

3 Likes