My script isnt getting all of the models, it only gets some of them

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

it so simple and yet it breaks idk why

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.

Hi, the models didnt even load when I put this.

Please provide more details then. Such as:

  • Explorer
  • Run the line print(game.Workspace:GetChildren()) before the loop to see if the fault lies in loading or the script.
  • Add a lot of debug prints (before my suggestion & after, during the if .. then loop, print model names, etc.

ViewportWorldModel (should have every cloned model from workspace in here)
Screenshot 2024-04-11 031607

workspace
Screenshot 2024-04-11 031618

I’m running into the same problem where some models don’t even load in the viewmodel.

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.

Is it a Local Script or Server Script?

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.

Hope this could help in some way.

1 Like

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

do this instead,

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)
1 Like

Yes I have used get descendants. I don’t think its a issue