Hi, I’m trying to get all of the models from the workspace and put them in a collective service, the problem is that its mostly getting a few of them and not all of them. How do i fix that?
for _,models in pairs(workspace:GetChildren()) do
if models:IsA('Model') then
collectiveService:AddTag(models,'ViewportModel')
end
end
If this script runs immediately when the game starts, it might be faster than the time required for the models to fully load into the game. To fix this issue, add these three lines at the top of your script:
if not game:IsLoaded() then
game.Loaded:Wait()
end
This will make your script run only after all the models have been loaded in.
Please try my other suggestions as well, being the print statements. These are very helpful, and may even help you solve it on your own.
If you are cloning models into the Workspace, these are not part of the DataModel which the game:IsLoaded() reads for. You should put a RemoteEvent that tells the script at what point all the models have been cloned, to which only after it received a signal will it collect all the models.
If that is also not it, I suggest adding a manual task.wait(5) (or more if your game has a lot of parts, just exaggerate the time to load) to the top of your script to possibly isolate and narrow the issue down. That way we can see if the problem is really just the loading or cloning.
If you have StreamingEnabled property set to true under workspace and it’s a Local Script, these models won’t load immediately after the player has joined or the player is further than StreamingTargetRadius.
One of the workarounds is to disabled StreamingEnabled under workspace, or add tags to these models in studio.
If you’re clonning those models from somewhere else you can just Add Tags there.
You can also try setting these models’ ModelStreamingMode property to “Persistent”.
Another way is to make every model have an Unique Name and keep those names in some kind of table. Then you can WaitForChild(ModelName) in that loop where you’re Assigning Tags. (if you’re clonning them yourself, otherwise you can’t predict what names those models will have). This is more advanced and I wouldn’t advise you to try it unless you are ready to spend some time and learn something new.
have you tried using GetDescendants instead of GetChildren? If that’s not the issue, then it’s probably from the models not being loaded yet, try using ChildAdded to add the tags when they load
local collectiveService = game:GetService("CollectionService")
for _,models in pairs(workspace:GetChildren()) do
if models:IsA('Model') then
collectiveService:AddTag(models,'ViewportModel')
end
end
workspace.ChildAdded:Connect(function(child)
if child:IsA('Model') then
collectiveService:AddTag(child, 'ViewportModel')
end
end)