Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I "GetChildren" twice in one model?

I have a model, that has 4 other models inside it, and then parts inside those models. I want to make the first model a variable with all the other models and parts with it. I tried doing:

local model = game.Workspace.model:GetChildren():GetChildren()

But, obviously, that won't work. So then I tried this:

local model = game.Workspace.model:GetChildren()
local realmodel = model:GetChildren()

But, that also doesn't work. Any ideas? All help is appreciated!

2 comments

0

GetChildren does not return a single object; it returns a table (a list) of objects. Calling GetChildren directly on a table of objects doesn't make sense. What you want is to go through the elements of that table and call GetChildren on those individual elements. This is exactly what happens in kingdom5's answer.

0

I get it now. So, that's why I'd need a "for i,v in pairs" right?

1 answer

✓ Accepted answer

The code above is the same except the second one just stores the result as a variable.

To do this kind of task you will need to use a loop:-

local list = {} -- stores the models objects

for _, v in pairs(game.Workspace.model:GetChildren()) do
	list[v.Name] = v:GetChildren()
end

-- you will then have a key value pair in the format of
-- model name = objects in the model

This method will not work if the model has the same name as it will overwrite the existing data.

I hope this helps.

2 comments

0

This is too complicated ._. Maybe you can explain a bit on it?

0

GetChildren returns an array of object in the model, we then loop through each item ( the models in the model), then set the key as the model name and get the children of that model for the value. the resulting format is {[some model name] = { array of parts for the model}}.

This discussion is preserved as it was. New answers, comments, and votes are closed.