Waiting for all (unknown number) of children to replicate

I need to wait for an unknown number of children to load in a given folder.

Is there still no API for this?

I only found one topic where a Roblox Dev mention that (at that time) there was no API
to detect this.

I cannot use :WaitForChild because I don’t know the names upfront.

1 Like

Are you copying a set amount of instances to another, or instead just waiting for them to load in at the beginning of the game? The first option would not be possible without knowing what you’re expecting, as logistically it can’t wait if it doesn’t know what it’s waiting for. However, if you do know how much you’re expecting, you can simply use a pairs loop to go through the folder and check that all the expected children are there, or instead just do #folder:GetChildren() >= expectedLoad.

I don’t fully understand your question, so correct me if you’re wrong, but I believe you are trying to detect when a folder loads all the expected children.

Detecting assets loading is all handled through ContentProvider

With this, you could use the PreloadAsync function with the instances you are adding to the folder, which yields the script until all of the instances are loaded in.

I don’t know how many I’m expecting, thanks anyway.

I don’t add the instances manually, so I cannot know which ones I need to wait for.

ContentProvider:PreloadAsync doesn’t wait for all children, this is seen clearly when I clone ~30k objects since it doesn’t wait for all of their children recursively.

ContentProvider:PreloadAsync doesn’t even work non-recursively for the 1st level of children (eg if I call it on a Folder then even the direct children of that Folder won’t all load…)

ContentProvider’s function only preloads asset IDs, not Instances. That’s a minus in our developing world.

Just check on the server how many there are and wait until it reaches that amount?

:wave:t2:Hello friend!
You could repeatedly wait 5 seconds to see if the total amount of children in the folder has changed. and if it hasn’t after 5 checks you end the code.

local Folder = YourFolder
local FolderChildren = #Folder:GetChildren()
local Attempts = 5
repeat 
wait(5)
if #Folder:GetChildren() > FolderChildren then
	FolderChildren = #Folder:GetChildren()
	Attempts = 0
else
	Attempts = Attempts + 1
end
until Attempts >= 5
print("Children have not changed in 25 seconds maybe I'm done loading?")

I solved it kinda like @dandcx said: I write num recursive children as an attribute to the Folder on the server and then on the client I wait for this attribute to load and then wait for that amount of children to load.

Thanks guys

1 Like