How do I yield until all descendants of an object have replicated from the server?

How do I yield my code until all descendants of an object have replicated from the server? The client does not know how many descendants there are.

I do not want to use StreamingEnabled.
I am detecting the object being added through CollectionService tags.

One way would be to have an attribute with the # of descendants on the object, then have the client check each frame with #GetDescendants() until this number is met, but this is horribly inefficient in my opinion.

Use a RemoteFunction that returns the number of descendants in an Instance.

That’s even more inefficient than an attribute.

On the client, maintain a table that tracks the number of descendants in a model. Instead of checking constantly, compare the client’s count with the server’s whenever the count updates, with a maximum of one check per second. Once the client’s count matches the server’s, further updates can rely solely on the client, eliminating any additional delays.

If you have the number of descendants that you expect you can measure the number once with GetDescendants and then update it on DescendantAdded.

And if you don’t have StreamingEnabled on already, which there is really no practical benefit to turning off especially if you are already using CollectionService to handle object behaviour, then the descendants should already exist when the object is parented to the world.

Ultimately I think this is more of an XY problem though, what are you trying to achieve by waiting for all the descendants of an object to exist? If there are descendants you directly need then it probably makes much more sense to wait for them explicitly, and if the structure of the descendant tree matters somehow then you are probably representing data using instances which is not really something you generally want to do if you’re going for efficiency.

Some more information about why you want to do this would probably lead to a better solution.

1 Like

What is your code? If you could send it we could help you better.

Also if you could somehow store all the descendants in a table, and then all the descendants you are replicating into a table, you could do something like

repeat task.wait() until presetArray == replicatedArray

Now I am not to sure with how efficient this is, but is really the only thing I can come up with right now that hasn’t been said already.