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

How to select a children of workspace if it is not a direct child?

Asked by
oggy521 72
6 years ago

I am making a board game on roblox, but I need to select something inside units but due to the messy placements, the units don't all have the same parent. I'm not sure how to explain it too clearly so this is an example of a problem which is similar.

Say if I had three items in the workspace with different names, (A,1 and !)

A has a child in it, which is a folder, let's call it B, and inside B is a bool value which we will call C

1 also has a child in it, called 2, and a bool in it called 3

! has only one child called ?, and that is a bool value

all bool values have "false" as default and all three bool value's names are the same

How do I change all bool values (C,3 and ?) to "true" without having to type game.Workspace.A.B.C.Value = true

but instead just do something like for i,v in pairs (game.Workspace.GetChildren()) do ...

2 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

:GetDescendants is what you're looking for, it gets the children of an instance, gets the children of those children, etc.

Coincidentally, the Tip of The Day may actually help your case, too.

0
YES, I forgot about it's name, that was just what I was looking for, thanks. oggy521 72 — 6y
0
Dont forget to accept this answer if it helped you. jackfrost178 242 — 6y
0
GetChildren() is the same as GetDescendants() @jackfrost178 DeceptiveCaster 3761 — 6y
1
GetChildren() gets the immediate children, while GetDescendants() gets the children of those children and so on, they are not the same. If they were, this question wouldnt have been asked, and one of the two API would have been deprecated by now. jackfrost178 242 — 6y
View all comments (3 more)
0
If there were only one sub-directory, they would do exactly the same thing. DeceptiveCaster 3761 — 6y
0
If there were 1 sub directory, GetChildren would just get that sub directory, not whats inside of it, you would have to reference that yourself, please see the roblox wiki on GetChildren and GetDescendants jackfrost178 242 — 6y
0
Yes you can use :GetDescendants() or if you know the exact name of the child you can do :FindFirstChild("NAME", true) luachef 61 — 6y
Ad
Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

FindFirstChild has a recursive parameter, which checks the descendants too. FindFirstChild would look like

local function FindFirstChild(instance, name, recursive)
    for _, child in pairs(instance:GetChildren()) do
        if child.Name == name then
            return child
        elseif recursive then --which makes it look for descendants, too!
            --return FindFirstChild(child, name, true)
            --I think it'd be more accurate with this.
            local found = FindFirstChild(child, name, true)
            if found then
                return found
            end
        end
    end
end

So, your code would be like

local values = {workspace:FindFirstChild("C", true); workspace:FindFirstChild("3", true); workspace:FindFirstChild("?", true)}

Hope this helps!

Answer this question