I dont understand the concept of why waitforchild exists and if its necessery, i would like to know if that is good for anything, should i just use SomeParent.SomeChild
or
repeat wait() until SomeParent.SomeChild
its not understandable even in the docs
This will throw an error “SomeChild is not a valid member of SomeParent” if the child does not exist.
The whole idea of WaitForChild is that it waits until the child exists and returns it when it does, with the option for a timeout. While this doesn’t have the most use cases on the server, it is particularly helpful on the client.
When assets replicate, their associated LocalScript
s can sometimes run before the assets are loaded. This means when they try to reference the assets they error. You used to have to make a custom WaitForChild function, but there are so many in the client that WaitForChild really becomes necessary.
--example of referencing a gui frame from the client at the top of a script
local frame = script.Parent.Frame --> 'Frame' is not a valid member of (parent here), this also gives a strict mode warning
local frame = script.Parent:FindFirstChild("Frame") --> nil
local frame = script.Parent:WaitForChild("Frame") --> Frame or "Infinite yield possible on ..."
In this example, for the first two, the script runs before the frame has loaded, resulting in issues.
I hope this helps!
So what if i just do repeat wait() until SomeParent.SomeChild ~= nil
will that do error too on the client?
its use cases explained in this video Roblox Devs, stop misusing :WaitForChild()!
also, if you tried to reference an instance that doesn’t exist with a dot . an error will be thrown into the console stopping the rest of your code from executing
FindFirstChild returns nil if an instance doesnot exist
-WaitForChild will Yield until the child is found or if it reached the timeOut
you will need to use waitforchild or findfirstchild in specific cases and in other cases all you need is to reference instances with just a dot . explained in the video
WaitForChild
is typically used when you’re not sure if the instance you’re looking for exists, so the script will wait until it finds the instance. It is useful to handle any potential delays the client may experience due to loading times or ping, as well as in other situations where you need to wait for an instance to exist before performing an action with it.
A typical example would be referencing the Humanoid
or Backpack
instances, which both require the use of WaitForChild
. This is because the character often spawns without the Humanoid
initially, as it may take a few milliseconds for it to be added and loaded. Additionally, the character may spawn before the new Backpack
is created.
LocalPlayer.CharacterAdded:Connect(function(NewCharacter)
local Humanoid = NewCharacter:WaitForChild("Humanoid" ,5)
local Backpack = LocalPlayer:WaitForChild("Backpack" ,5)
--doing: "local Instance = Thing.Instance" may throw an error because it doesn't exist yet
end)
While it works, this method is much less efficient for performance and can significantly increase the amount of code for such a simple task.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.