I’m having an issue with my custom backpack GUI which causes tool previews to not be complete due to some of it descendants not having time to replicate. I’ve tried using WaitForChild but this only works on a specifc children as opposed to a whole bunch of them.
Would there be a way to wait for all descendants to be loaded or an alternative fix?
What you tried is actually the most common way of managing replicated instances. It definitely looks ugly and is tedious to set up but it works. But some alternatives:
The game:IsLoaded() method checks if the client’s game has finished replicating all the instances after first joining the game, ignoring AssetId fletching. If the method returns true, it should guarantee the instance’s availability. You can use a simple loop to wait.
repeat task.wait()
until game:IsLoaded()
However, if you are waiting for something to replicate during the game (an instance that’s broadcasted upon request), then it should be as simple as setting the Model’s streaming mode to atomic. You only need to run :WaitForChild on the parent Model itself, and all of the children instances are immediately available when the parent is.
Third option is you manually counting the descendants and waiting until it reaches a certain number. Very rudimentary, but trivial. You could use an attribute to record the number of descendants and then a yielding function to force the thread to wait.
local function waitForDescendants(instance: Instance)
local amount: number = instance:GetAttribute('DescendantCount')
repeat task.wait()
until #instance:GetDescendants() == amount
end