You got it right, its especially useful in scripts accessing replicated storage,etc as the contents of replicated storage may take longer to load than the script.
Will wait forever for an object with the specified name. By default, if the object isn’t found within 5 seconds, an Infinite yield possible warning will appear in the Output to let you know that the object wasn’t found. This will stop the code below it from continuing until the object is found. So if the object were to appear 1000 seconds later, only then would the code be able to continue running.
However, you can add an optional “timeout” to force the code below it to continue running, even if it doesn’t find the object. So if you did something like Instance:WaitForChild("Part", 10) and the object named “Part” was not found after 10 seconds of waiting, the code below it would continue running instead of waiting forever.
Will always look for an object with the specified name and not the name of a Property. If you created a part called “Anchored” for example, using Instance:WaitForChild("Anchored") will look for an object with that name, whereas using Instance.Anchored will try to access the Anchored property of the Instance, if it has it.
Very commonly used to look for objects in ScreenGuis, as certain gui elements might not have loaded in for the player at the time a LocalScript is trying to look for something.
Similar to :WaitForChild(), except that this one does not wait until the object with the specified name exists. If the object exists, it will return it, but if it doesn’t find it, it will be nil (which is another way of saying “nothingness” / something doesn’t exist).
This is often used to make sure that certain code will only run if a specific object exists. Or, it can even be used to run separate code in the case that an object does not exist (which is especially useful if there’s a super important object that needs to exist for the rest of the code to work properly). Here’s an example:
local Folder = workspace.Folder
local object = Folder:FindFirstChild("TestPart")
if object then
print("An object named 'TestPart' exists in the folder!")
elseif not object then
warn("Creating a new object because 'TestPart' did not exist in the folder")
local newObject = Instance.new("Part")
newObject.Name = "TestPart"
newObject.Parent = Folder
object = newObject
end
print(object.BrickColor)
There are other variations of Instance:FindFirstChild() that can be used for more specific purposes, including:
(The differences between each one are explained on that Roblox Creator Documentation site page)
Dot Indexing
Will start by looking for a Property with the given name. If it doesn’t find that, it will look for an object inside of the Instance with the given name.
For example, if you have a Part in the Workspace and you type workspace.Part.BrickColor, that will refer to the BrickColor property of the part, even if there’s an object with the name of “BrickColor” placed inside of the part. You can use Instance:FindFirstChild() or Instance:WaitForChild() to specifically look for an object with that name, without it accidentally referring to its property.
If you were to type Part.BRICKCOLOR, that would instead look for an object inside of the part with the specific spelling and capitalization of “BRICKCOLOR”, because it did not exactly match the same spelling and capitalization as its BrickColor property.
It will immediately error if the Instance does not have a property with the given name, or if an object with the given name does not exist directly inside of the Instance.
This means that if you tried to look for Instance.ExamplePart, the script would not find a property with that name, so it’d look for an object called “ExamplePart”. If it doesn’t find an object inside of the Instance with that name, it will error. Even if the “ExamplePart” object is added into the Instance 1 second after the script tried to look for it, the script will still error and not continue. A property or object with the exact same spelling and capitalization has to exist at the exact moment the script tries to look for it in order to avoid an error when using dot indexing.
As a result, it is generally advised to use Instance:FindFirstChild() or Instance:WaitForChild() when looking for objects that have the same name as a property and / or are not 100% guaranteed to be in the exact spot at the specific moment in time that you expect it to be in the game.