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

How to get children of children?

So if I had a model that had models within it. What would I do to get everything in those models? Would I do?

game.Workspace,Model:GetChildren():GetChildren()

I am really unsure of this.

Thanks. Antux

1 comment

0

Do you only want childrens' children? or do you also want childrens' childrens' children? childrens' childrens' childrens' children? etc

2 answers

The best way of getting children is by doing this:

for i,v in pairs(game.Workspace.ModelName:GetChildren()) do --gets children in the model
	if v:IsA('Model') then --Checking if whatever is in ModelName if it has a model in it
		for i,x in pairs(v:GetChildren()) do --Gets the children of the models, if there are any.
			x:Destroy() --This is just an example, not sure of what you are wanting to do here.
		end
	end
end

Hope this helps, let me know if there are any errors because I can't test.

for i, v in pairs(game.Workspace.Model:GetChildren()) do
	--v/variable 2 is the object
	--i/variable 1 is index
	--put your code here

	for index, obj in pairs(v:GetChildren())
		--works exactly same as above you can't use the same name on the variables!
		--put your code here
	end
end

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