Do I always have to use a WaitForChild() after a Clone()?

I have an ImageLabel with a child TextLabel, inside StarterGui:


I purposely leave these items inside StarterGui as templates to populate a ScrollingFrame that has already been replicated inside Players.

local ItemOrig 	= game.StarterGui.ScreenGui:FindFirstChild("ImageLabel", true)
local Item = ItemOrig:Clone() -- from StarterGui (Item is the cloned "ImageLabel")
Item.Parent = MyFrame -- in game.Players
Item.Image = someValue
Item.TextLabel.Text = someText -- here SOMETIMES I get an error

The last line sometimes generates an error, and looking inside Players, there is only the ImageLabel (TextLabel) is not there yet.

Do I always have to use a WaitForChild after a clone?

It is not the clone that is erroring, but probably findfirstchild returning nil since the label hasn’t loaded in

An “error” doesn’t help anyone other than saying you’ve got a problem without telling what it might be. And no you don’t need to use WaitForChild if the object already exists like when you clone it it should already exists.

That’s my question: like WaitForChild, will Clone yield the current thread until the cloned object is fully loaded?

Unsure if it’ll yield but it’s slower cloning an object rather than having it already exist and cframing it to the position needed.

Well, I got the error again and it’s something like TextLabel is not a valid member of ... (the Players replicated Gui).

There, only ImageLabel was cloned (its child TextLabel was not):


So I had to create a WaitForChild for the TextLabel:

Item.Image = someValue
local T = Item:WaitForChild("TextLabel")
T.Text = someText -- now it's working...

So, answering my own question:

If a cloned object has children, you must YES use WaitForChild for each child of the cloned parent :point_left:

1 Like