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

Is there a neat way of getting every Instance in Workspace?

Asked by 7 years ago

Well, the Title says it. My target was about getting all Instances in workspace that has a ClassName value of "ClickDetector"

I know there are many ways to do so, But is there and short and simple solution for it? Because :GetChildren only shows what was selected.

0
What do you mean by ':GetChildren only shows what was selected'? honeygold13843 136 — 7y
0
example: workspace.GetChildren(), only shows what is childrens on workspace, not every children on workspace. NathanAdhitya 124 — 7y

2 answers

Log in to vote
2
Answered by 7 years ago
Edited 7 years ago

This is a fun one!

So, I'm going to loop through all the descendants and children in workspace, no matter how deep it is. I assume this is what you wanted.

First, let's make a blank function to work with,

function Paint(x)
    --Loop through workspace
end

Paint(workspace)
We send it what we want it to loop through.

Great. So let's loop through whatever is sent to this function using a pairs loop. If you don't know what a pairs loop is, he's a link to more info about loops.

function Paint(x)
    for i,v in pairs(x:GetChildren()) do
        -- Do stuff
    end
end

Paint(workspace)

But that only gets the children.

Yes, but if we want it to loop through everything, all we have to do is check if v, the child of the thing we're looking through, has any children, and if it does, call the function again through the method of recursion.

I'll be checking if v has any children using the next function.

function Paint(x)
    for i,v in pairs(x:GetChildren()) do
        if next(v:GetChildren()) ~= nil then
            Paint(v)
        end
    end
end

Paint(workspace)

And that will loop through everything in workspace!

Why did you call the function paint?

I actually made this script not too long ago to paint all the parts in workspace. My script looks like this,

local function Paint(x)
    for i,v in ipairs(x:GetChildren()) do
----------------------------------------------------------------
        if v:IsA("Part") then
            v.BrickColor = BrickColor.Random()
        end
----------------------------------------------------------------
        if next(v:GetChildren()) ~= nil then
            Paint(v)
        end
    end
end
Paint(workspace)

Doesn't take much to do what you want with this.

Is it laggy?

Not really. However, if you have tens of thousands of parts in your game, you might feel a lag spike.


I hope I helped. If I didn't do a good job of explaining, ask me what you're confused about and I'll make sure to do my best to provide an answer.

Good Luck!

If I helped, don't forget to accept this answer!
0
Let's see, looks perfect, ill test it ASAP. NathanAdhitya 124 — 7y
1
Perfect! What i was exactly looking for! NathanAdhitya 124 — 7y
1
Yay :) User#11440 120 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

http://wiki.roblox.com/index.php?title=API:Class/Instance/DescendantAdded

0
Hmm, Not really what i was looking for, i was looking to scan, not to see what was added after the event is attached. NathanAdhitya 124 — 7y

Answer this question