GetChildren is not getting BaseParts

I haven’t updated my game in about a year and I noticed animations are broken. I’ve used print() to narrow down the issue and it’s a strange one.

	local nextFunction, allParts, index = pairs(model:GetChildren())

	for i = 1, #allParts do
		local part = allParts[i]

		if part:IsA("BasePart") then
			doAnimation(part)
		end
		if part:IsA("Model")then
			for _, desc in pairs(part:GetChildren()) do
				if desc:IsA("BasePart") then
					doAnimation(desc)
				end
			end
		end
	end

This code passes a model in and does an animation up to two levels deep on BaseParts. The problem is, doAnimation() is never fired because BaseParts are ignored on GetChildren().

If I pass a model in that looks like:

-Model
   -BasePart

The for loop will never run at all.

If I pass a model in that looks like:

-Model
   -Model
      -BasePart

The first for loop will run, but the 2nd for loop will not. GetChildren() is not returning any parts on models.

Does anyone have any ideas on what change to Roblox within the past year would be causing this?

1 Like

You could use :GetDescendants() to cycle through everything under the given model(s) and filter through the BaseParts.

for index, descendant in pairs(model:GetDescendants()) do
	if descendant:IsA("BasePart") then
		doAnimation(descendant)
	end
end
1 Like

I only want to go through two levels – not infinitely. Some of my models have a lot of nested parts and models.

I also don’t even know if GetDescendants() would have the same issue. I will have to test that.

1 Like

I’m not surprised that GetDescendants() also does not get BaseParts. It seems to get other classes, like humanoids, but will not do BaseParts. Very puzzling.

I just noticed the model objects are cloned objects. I wonder if it has something to do with that.

1 Like

Try to do descendant:IsA("Part") instead of "BasePart"

1 Like

This wouldn’t make a difference, because the for loop isn’t being run at all. The BaseParts don’t exist in the model variable for some reason.

Honestly I don’t understand you’re not just using a for i, v in pairs(model:GetDescendants()) do loop instead of

local nextFunction, allParts, index = pairs(model:GetChildren())
for i = 1, #allParts do

If the loop is working correctly, the only other possibility of your parts not existing inside of the model would be that they were created on the client and you’re looping on the server.

Could be a syntax error.
I’d do this to only go 2 levels deep:

for i, part in pairs(model:GetChildren()) do
    if part:IsA("BasePart") then
         doAnimation(part)
    elseif part:IsA("Model") then
         for _, desc in pairs(part:GetChildren()) do
             if desc:IsA("BasePart") then
                doAnimation(desc)
             end
        end
    end
end

Also check if they’re all regular parts.

1 Like

All the code is correct. I know it’s a weird way to do it, but it works. The problem lies in the model variable not containing the parts for some reason.

The model is cloned on the server and then passed to the client for this loop. I’m looking through a year of release notes to see if I can find anything, but it’s a lot.

Blockquote
Could be a syntax error.
I’d do this to only go 2 levels deep:

I did end up changing the code over to this simply because it’s cleaner and easier to maintain. :+1:

Providing more context…

The model is cloned in the workspace (server-side):

Objects[ThingMade.Name] = ThingMade:Clone()

Then sent to a remote in replicatedStorage to get it on the client-side:

local model = Objects[item.Object.Value]
local tycoonAnimation = replicatedStorage.Remote:WaitForChild("TycoonAnimation")
tycoonAnimation:FireClient(owner.Value, model, initialT)

Then the client-side does the for loop as mentioned above.

Again, this was all working fine, but an update has broke it. I appreciate all the help!

1 Like

Please mark it as a solution, as the topic is solved.

This is no solution yet, sadly.

If you are finding children of a model’s children, you have to use GetDescendants Instead of GetChildren. Just replace those lines and you should be good.

That never used to be true, is that a new change?

Regardless, changing to GetDescendants fails to get any BaseParts from the model.

Did you set the model’s parent to the Workspace? It’s a bit hard to understand the problem without all the code. Also are the parts in the model BaseParts or something else like MeshParts?