Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

: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.

7 comments

0

YES, I forgot about it's name, that was just what I was looking for, thanks.

0

Dont forget to accept this answer if it helped you.

0

GetChildren() is the same as GetDescendants() @jackfrost178

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.

0

If there were only one sub-directory, they would do exactly the same thing.

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

0

Yes you can use :GetDescendants() or if you know the exact name of the child you can do :FindFirstChild("NAME", true)

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.