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)
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
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!
2 comments
What do you mean by ':GetChildren only shows what was selected'?
example: workspace.GetChildren(), only shows what is childrens on workspace, not every children on workspace.