Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a way to find all parts?

Asked by 10 years ago

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.

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. jav2612 180 — 10y

2 answers

Log in to vote
2
Answered by
duckwit 1404 Moderation Voter
10 years ago

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!

Ad
Log in to vote
0
Answered by
Tiranin 45
10 years ago

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.

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! ;) duckwit 1404 — 10y
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. Tiranin 45 — 10y
0
Or did you mean that even BaseParts can be in BaseParts, because your comment didn't make much sense..? Tiranin 45 — 10y
0
Yes, apologies, that's what I meant. You need to print the name of a BasePart and pass it to the Find() function. duckwit 1404 — 10y
View all comments (6 more)
0
That's an easy thing to do! But what are the chances of BasePart having a BasePart... Will edit anyway. Tiranin 45 — 10y
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 . duckwit 1404 — 10y
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.  User#348 0 — 10y
0
I was first. :< Tiranin 45 — 10y
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. duckwit 1404 — 10y
0
You beat him by one minute... User#348 0 — 10y

Answer this question