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

Any way to ":FindALLChildrenWhichIsA("Part")" or ":FindALLChildren("Part")" ?

I have a bunch of models with ClickDetectors that do things. All with the same names, all duplicated models.

Currently, I have it working where each model has its own script inside, but I'm trying to cut down on number of scripts, to avoid 200+ scripts. So I'm trying to have one script apply to all the models.

I've got code the following so far:

local SELECTED = game.Workspace.Adam.equin:FindFirstChild("Right Arm")
local BUTTON = SELECTED:FindFirstChildWhichIsA("ClickDetector")
local SHIRT = BUTTON.Parent.Parent:FindFirstChildWhichIsA("Shirt")

BUTTON.MouseClick:connect(function(Player)

print("Hello")

end)

However, this only works on one of the models since it's only finding the first child... mind pointing me in the right direction? -please explain like I'm 5, I'm an inexperienced coder.

Note: I've looked into GetChildren() and loops, but none of that really makes too much sense to me.

3 answers

✓ Accepted answer

(Seems like I was beaten to the answer by @Vulkarin, but here it is anyway since it's already typed out, and it's a little different):

You could create a ModuleScript that can be require()d from any script by adding a ModuleScript named "GetChildrenWhichAreA" to ReplicatedStorage...

return function(parent, class, recursive)
    local descendants
    if recursive then
        descendants = parent:GetDescendants()
    else
        descendants = parent:GetChildren()
    end
    local selected = {}
    for i, descendant in ipairs(descendants) do
        if descendant:IsA(class) then
            table.insert(selected, descendant)
        end
    end
    return selected
end

...and then using it like so:

local getChildrenWhichAreA =
    require(game.ReplicatedStorage.GetChildrenWhichAreA)

-- Let's get all the BaseParts inside the folder
local folderWithStuff = game.Workspace.Folder
local childBaseParts = 
    getChildrenWhichAreA(folderWithStuff, "BasePart")
local descendantBaseParts =
    getChildrenWhichAreA(folderWithStuff, "BasePart", true)

There is a function literally known as "IsA" which works for what you need you can do something like

-- gets children only
children = model:GetChildren()

-- gets children and their children, etc
children = model:GetDescendants()

-- then you can sort through them to see if they are baseparts
for _, child in pairs(children) do
	if child:IsA("BasePart") then
		print(child.Name.." is a basepart")
	end
end

Now know that this will find BaseParts and not just "Part" specifically so if you would like to do that instead you can do

for _, child in pairs(children) do
	if child.ClassName == "Part" then
		print(child.Name.. "is a Part")
	end
end
local SELECTED = game.Workspace.Adam.equin:FindFirstChild("Right Arm")
for _, v in pairs(SELECTED:GetChildren()) do 
	if v:IsA("ClickDetector") then 
		local BUTTON = v 
	end 
end
for _, v in pairs(BUTTON.Parent.Parent:GetChildren()) do 
	if v:IsA("Shirt") then 
		local SHIRT = v 
	end 
end

BUTTON.MouseClick:connect(function(Player)

print("Hello")

end)

I think this is what youre looking for.

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