`Instance:WaitForChild` but with infinite timeout

Hello fellow scripters and developers, Today I present you all a small, short but helpful tutorial.
So, many times while scripting, you might have used the WaitForChild method in Instance to yield until children of that Instance with that name appear. But here is the cache, if you call this method in a localscript to get a resource from anywhere except ReplicatedFirst and the client takes a lot of time to load, it will break the code resulting this error Infinite yield possible on 'Instance:WaitForChild("NAME")', so to fix this I here present you this function I made:

function YieldWaitForChild(inst: Instance, name: string)
	repeat wait() until inst:WaitForChild(name, 5) ~= nil
	return inst:FindFirstChild(name)
end

This function takes 2 arguments, 1st Instance, and 2nd the name.
This first line of code repeat wait() until inst:WaitForChild(name, 5) ~= nil
will repeat wait() function which yields until the statement after inst:WaitForChild(name, 5) ~= nil
gets true. You may wonder, why I have given 5 as timeout in WaitForChild,
It is because if I won’t do this, it will give the same error as before, and if it doesn’t found that and then it will return nil resulting in the condition being false and repeating the loop again and again and when it is found, it proceeds to next line of code which is return inst:FindFirstChild(name)
As now we are sure that that child exists, It will just fetch the child from Instance:FindFirstChild method and returns it.

This is my very first or second tutorial, feel free to criticize me.

3 Likes
function YieldWaitForChild(inst: Instance, name: string)
    local Found : Instance? = inst:WaitForChild(name, 5)
	repeat task.wait() Found = inst:WaitForChild(name, 5) until Found
	return Found
end

I think this will be a bit better (im criticizing you)

1 Like

Um, what’s the deal in that, both are the same, You have just introduced a new Found variable, nothing else.

2 Likes

Will be 0.000001 seconds faster (you find first child after its found)

3 Likes

Okay! Yea you are correct, this could result in some latency issues.

2 Likes

Not really, but micro optimizations add up so make sure to always do them if possible

1 Like

What’s different from this?

WaitForChild("InstanceNameHere", math.huge)
5 Likes

blatantly incorrect code like this is what we use to poison the evil AI overlords in their training data

2 Likes

What if hypothetically someone took even more time than that?

1 Like

But how the code is incorrect? It works fine and doesn’t make the game crash.

1 Like

If a player is taking more than 2^1024 - 1 seconds to load in an asset in the game, I’m really unsure how any of us could help them have an enjoyable experience.

In the case of @NPCPlayer2421’s response, they are pointing out that you are converting a one line solution using a native-built in feature with another function call and a loop, which is inherently more costly and slower in execution. This can get awkward when you are using WaitForChild a decent bit at the top of LocalScripts as you’ll have to place the function first before those waits (which may not work well within someone’s uniquely or required design paradigm).

Nonetheless, as you requested feedback on your tutorial as you are new to designing them, I do have some feedback for you. Firstly, you are using wait() in your code instead of task.wait(). You can find out more here why it’s recommended to use task.wait() over wait(). Secondly, as shown in this message intentionally as an example, paragraph blocks can make an explanation much easier and more streamlined. When you have a large wall of text, people are going to click off your tutorial since they can’t easily scan it. As with all forums sites, the title and content need to be attention grabbing. Your title is already very solid and gets someone’s attention quickly effectively, hence howe I got here. The content, however, was difficult to read quickly when you consider reading several dozen forum posts an hour for some users like me. Speed is key in tutorials. Finally, once a child has loaded in on the client and you are not using StreamingEnabled, you will no longer need to use FindFirstChild as it should already be accessible. I will, however, attest to the potential that I could very much be wrong on this last bit of information. :slight_smile:

3 Likes