GetChildren() advanced list of objects

i’m currently using a simple script to switch from

workspace.Folder:GetChildren()

--output vvv

[1] = One
[2] = One
[3] = Two
[4] = One
[5] = One
[6] = Two

to

[One] = 4
[Two] = 2

using


local otherTable = {"foo", "foo", "bar", "foo", "bar"}
local count = {}
for _,v in ipairs(otherTable) do
    -- count[v] may be nil.
    count[v] = (count[v] or 0) + 1
end

From another tread, it works fine and i have no problems about it but i would expand his selection because every part inside the workspace.Folder every part has an attribute called "Attribute_One" wich changes on every part

I would like to recieve an output of the amounts based on the Attributes value match

let’s say i have 5 parts with this names and attributes

One, Attribute_Value = 5
One, Attribute_Value = 5
One, Attribute_Value = 4
One, Attribute_Value = 4
Two, Attribute_Value = 5

The expected output would be

[One] = 2, 5
[One] = 2, 4
[Two] = 1, 5
"Names, Amount, Attribute value"
3 Likes

I think this is what you want. Not tested. Note this does not check the type of attribute before updating amount.

local function GetCountOfObjects(objects)
    local count = {}

    local function GetValue(i, v)
        for index, value in ipairs(count) do
            if value.name == i and value.value == v then
                return value
            end
        end
    end

    for i, v in ipairs(objects) do
        for name, value in pairs(v:GetAttributes()) do
            local get = GetValue(name, value)

            if not get then
                get = {
                    name = name,
                    value = value,
                    amount = 0
                }
                count[#count + 1] = get
            end

            get.amount += 1
        end
    end

    return count
end

-- Print in your format
local t = GetCountOfObjects(workspace:GetChildren())

for _, v in ipairs(t) do
	print(('[%s] = %d, %s'):format(v.name, v.amount, tostring(v.value)))
end
3 Likes

I’m using a new place to test it out with this parts
1
The output actually doesn’t return a list based also on Names but only on amount and values
2
This is the script copy+pasted in the new script


Maybe i’ve setted it wrong and needs the other script?

1 Like

Do you want the objects name and not the attribute name? and wdym by list?

i want the list sorted by names, amount and values like this

1 Like

just change this line

get = {

    name = v.Name, -- from name

    value = value,

    amount = 0

}

this will use the objects name.

Now it prints them correctly but the amount is always 1 and prints them like this
Immagine 2022-01-17 012551

1 Like

oh i forgot change this to

local get = GetValue(v.Name, value) -- from local get = GetValue(name, value)

I have been thinking about a solution for about two days before turning to the forum, I thank you infinitely for your help and sorry if I was not clear in the explanation of the thread

1 Like

Its always good to try to figure it out by yourself.