I always use Workspace:WaitForChild("Model")
when referencing assets on the client, but what happens if I need to get all the children of an asset? I would need to use the :GetChildren()
function, but can I assume that if the parent model is returned from :WaitForChild("Model")
, its children will also be fully loaded and returned with :GetChildren()
? How do I know when it’s safe to use :GetChildren()
?
GetChildren() returns a table of the Instance childrens
No, you should use :WaitForChild() on any of a child instances children if they need to be loaded before script execution.
Parent > Child1 > ChildOfChild1
Just because Child1 has loaded that doesn’t always mean that ChildOfChild1 has loaded too.
Parent1:WaitForChild("Child1"):WaitForChild("ChildOfChild1")
When is it safe to use :GetChildren()
though? I can’t use wait for child because I need a table of all the children at once. If the model is partially loaded that table may be incomplete which would break the code.
:GetChildren() fetches all of the children of an instance.
So :GetChildren() is a yielding function? The devhub doesn’t say that it is.
Without knowing the internal definition of “GetChildren()” since Roblox isn’t open source, I wouldn’t know.
It’s possible that it runs in its own thread (and is incredibly fast), you’d likely need to query someone who works at Roblox to know.
It can’t run in its own thread.
I just wish Roblox would be more clear about how their replication system works. I need to use :GetChildren() within a local script located in ReplicatedFirst, but I don’t think it will work the way I expect it to. It seems the only solution would be to wait until the initial load of the game has finished.
However, when new assets are replicated across the client server boundary during gameplay, such as a map, how am I going to know when its safe to use :GetChildren() on map objects. I may have to reference count every part in the map to determine when it loads fully, unless there is a better way?
maybe use GetChildren() and then put it in a for loop and use WaitForChild() with the name of the children that you are currently on?
local mymodel = game.ReplicatedStorage:WaitForChild("myModel")
for _, v in pairs(mymodel:GetChildren()) do
local loadedm = mymodel:WaitForChild(v.Name)
loadedm:Clone().Parent = workspace -- example
end
IsLoaded() isn’t a method of instance?
However this may work for initial asset loading, but what about assets that are loaded during gameplay such as a new map. Does anyone know how to wait for the asset to finish loading before indexing its children? Does it need to be manually done?
Another option is to use SetAttribute(‘ChildCount’) on the parent object on the server. Attributes seem to be replicated with the object itself, so when the parent object lands on the client you know how many children to wait for. Then you can listen to the ChildAdded event, and when the actual number of children matches your manually-set ChildCount attribute, you know all the children are loaded.
I thought this would require a manual solution. Too bad it isn’t any easier
oh yes, right. i’m sorry, lol.