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

Is there a way to find all parts?

I was wondering if there was a way to find all parts, regardless if they are in models or not. I know you can do this:

local Model = nil
for i, v in pairs(Workspace:GetChildren()) do
	if v:IsA("BasePart") then
		print(v.Name)
	elseif v:IsA("Model") then
		Model = v
		for i, v in pairs(Model:GetChildren()) do
			if v:IsA("BasePart") then
				print(v.Name)
			elseif v:Isa("Model") then
			--and so on and so forth
			end
		end
	end
end

I just want to know if there is a simpler way to do this.

1 comment

0

You can create a function to use :GetChildren() on any models you encounter and have the function return if there are any models in that list. then you can just repeat the function until there are no more models.

2 answers

✓ Accepted answer

Yes, there is - and it's also a very powerful concept to learn when it comes to programming and Computer Science.

It's called Recursion

Here's an example that matches your question, though, it prints all the Part Instances of the Instance that you send to the function.

function PrintParts(instance)
	if instance:IsA("BasePart") then print(instance.Name) end
	for _,child in pairs(instance:GetChildren()) do
		PrintParts(child)
	end
end

--Print all the parts in workspace
PrintParts(workspace)

--Print all the parts in Lighting
PrintParts(game.Lighting)

Enjoy!

Use functions for extra boost!

function Find(part)
	for i, v in pairs(part:GetChilren()) do
		if v:IsA"BasePart" then
			print(v.Name)
		end
		Find(v) -- is this madness?
	end
end

Find(Workspace)

This is a very simple, yet complex search technique! When you call function Find and declare where it should start (etc. Workspace), it will search through children of part and if the children is/are "BasePart", it will print its/their name. But, if it's not a "BasePart", it calls function Find resulting in searching everything until there is nothing to search.

If you're only into searching Models, then just change else to elseif v:IsA"Model" then.

10 comments

0

You're on fire, Tiranin! But unfortunately there is a slight problem here, you need to run Find(v) even if it's NOT a BasePart, because even non-BaseParts can have BaseParts as children, and we want to know where ALL the parts are! ;)

0

Pardon, but the lines 3,4,5,6 and 7 cleary state that if the instance is BasePart that it should print its name, otherwise it will continue searching through the instance.

0

Or did you mean that even BaseParts can be in BaseParts, because your comment didn't make much sense..?

0

Yes, apologies, that's what I meant. You need to print the name of a BasePart and pass it to the Find() function.

0

That's an easy thing to do! But what are the chances of BasePart having a BasePart... Will edit anyway.

0

Programming technique has little to do with chance :) Yes, it's easy, but it's not necessarily the sort of detail that the OP would spot .

0

You guys are both right! Thanks for the help! Unfortunately I can only accept one answer, but upvotes to both of you! I guess I didn't realize you could use a function in the same function. 

0

I was first. :<

0

There is actually a difference between our answers - mine takes into consideration the instance passed to the function as a potential Part, but Tiranin's only counts the children. If you passed it a Part with Part children, only the children would be considered - depends on your needs though.

0

You beat him by one minute...

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