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 get the models, in a model, and the the parts in those models?

I asked this before, but it was too complicated. To be specific, I have this:

local outter = game.Workspace.all.outter:GetChildren()
local second = game.Workspace.all.second:GetChildren()
local third = game.Workspace.all.third:GetChildren()
local last = game.Workspace.all.last:GetChildren()
local all = game.Workspace.all:GetChildren()

The "all" variable doesn't work, people say. And I tried it, and it doesn't work. Can someone, if they know, tell me the most SIMPLEST way, to "GetChildren()" twice. A models children, and those models children. All help is appreciated!

1 answer

✓ Accepted answer

GetDescendants

It's not a Roblox function, but that's because Roblox is bad. Here's the Freya implementation of it.

GetDescendants = function(i)
    	local queue = {};
    	local returns = {};
    	local v = i;
    	while v do
        	local t = v:GetChildren();
        	for i = 1, #t do
        		local v = t[i]
        		returns[#returns+1] = v;
        		queue[#queue+1] = v;
        	end;
        	v = queue[#queue];
        	queue[#queue] = nil;
    	end;
    	return returns;
end;

Where i is the model to get all the descendants of it.

5 comments

0

I wish I understood this ._. I don't like implementing code I don't understand. That would defeat the purpose of me learning. Is there any other way of doing this, perhaps? Maybe with a "for i,v in pairs" loop? If not, that's fine, I'll accept your answer, since I can use this later on. Thanks for trying to help out!

0

It gets all the children, and then it gets the children's children, and then the children's children's children, etc etc

0

If you wanted to just do it twice, just loop through the other lists and add them to a table lol

0

Unrelated to the question, but I like this implementation a lot, since I automatically reach for recursion to solve these problems. Not something I would have thought of.

0

Iterative solutions are always faster than recursive solutions. Where possible, avoid recursion - Stacks and queues are a good way to do it.

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