:WaitForChild usage

So I’m creating an ATM system and have a lot of variables. The script is a LocalScript which is inside of a GUI. Do all of these variables need :WaitForChild() or is FindFirstChild() more appropriate in this case? Or perhaps just directly referencing the Ui instances without FindFirstChild() or WaitForChild()?

I find myself using :WaitForChild() in all of my scripts and am wondering if maybe it’s not necessary. Like do I need to use it when referencing a RemoteEvent in ReplicatedStorage from the client/server? I hope that makes sense.

Code snippet:

local main = script.Parent.Main
local confirm = main:WaitForChild("Confirm")
local depositButton = main:WaitForChild("Deposit")
local inputField = main:WaitForChild("InputField")
local withdrawButton = main:WaitForChild("Withdraw")
local accountDisplay = main:WaitForChild("AccountDisplay")
local walletDisplay = main:WaitForChild("WalletDisplay")
local errorGui = script.Parent:WaitForChild("ErrorMessage")
local errorMessage = errorGui:WaitForChild("Background"):WaitForChild("WarningLabel")

local selected = script.Parent:WaitForChild("Selected")

Side note: These variables are at the top of a pretty lengthy script. I was wondering if putting the variables inside a ModuleScript would be better for efficiency and readability. Thoughts?

(I’m sorry this post is worded so weirdly, it’s been a long day and I’m mentally fatigued)

1 Like

The client needs to wait because of loading on the workspace. FindFirstChild is instant and will not wait.

3 Likes

Well it depends. for me, if the server just starts I typically use :WaitForChild() just in case whatever I’m looking for hasn’t quite had the time to load into the server. but if I know the object is already loaded in, i’ll use :FindFirstChild() to get the variable. WaitForChild() gives the variable time to load in without causing immediate error.

3 Likes

FindFirstChild isn’t really mean to be a replacement for WaitForChild as they have a different purpose. FindFirstChild is meant to be used when the Instance has a chance not to be there (because it has been destroyed for example) so even server Scripts need to use it occasionally. WaitForChild is used in LocalScripts because Instances might not have loaded yet for the player, but you need to use them for something inside of the script. Here are examples of how they work (not exact examples but the functionality is there):

FindFirstChild example:

local success, instance = pcall(function()
	return parentInstance[childInstanceName]
end)

if success then return instance else return nil end

WaitForChild example:

local instance

repeat
	instance = parentInstance:FindFirstChild("ChildInstanceName")
	task.wait()
until instance

return instance
1 Like

Just adding on to what @JohhnyLegoKing said, FindFirstChild() should also not be used unless necessary as it can create lag when used a lot, in a loop for example.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.