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

Can I get the # of :GetChildren but only with children that meet a selected criteria?

Asked by 3 years ago

Is it possible for me to get the number amount of :GetChildren that meet a selected criteria? I want to be able to get the number of children (with #children) but only if the child is a folder.

2 answers

Log in to vote
1
Answered by
rabbi99 714 Moderation Voter
3 years ago
Edited 3 years ago

It is not possible with just :GetChildren, however you can make your own table.

function GetChildrenWithName(object, name)
    local ChildrenWithName = {}
    for i,v in pairs(object:GetChildren()) do
        if v.Name == name then
            table.insert(ChildrenWithName, v)
        end
    end
    return ChildrenWithName
end

local Amount = #GetChildrenWithName(workspace.Folder, "Part")
Ad
Log in to vote
0
Answered by 3 years ago

You can do this with :GetChildren() and a variable

local Count = 0
for i, v in ipairs(game.Workspace:GetChildren()) do
    if v:IsA("Folder") then
        Count = Count + 1
    end
end
print(Count)

Count should print the amount of children that meets your selected criteria

Answer this question