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