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.
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)
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)
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!
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.
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!
http://wiki.roblox.com/index.php?title=API:Class/Instance/DescendantAdded