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

How do I print the number of {} table?

Asked by 5 years ago
    local instances = {}

for _,child in pairs(workspace.Servers:GetChildren()) do
    if child:IsA("Folder") then
        table.insert(instances, child)
    end
end

print(unpack(instances));

In the output, it would say, "Folder Folder" But I Want it to become "1 2" counting the specific objects instead of nouning them. I've been trying to go in the wiki but I can't really find anything... Thanks in advance!

0
Could you accept my answer if it helped you. programmerHere 371 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Use the unary length operator, #.

This is all you need. The unary length operator gets how many elements are in a table.

So don't unpack it, you can instead just get the length:

print(#instances)

However this won't print 1 2, it will just print 2. If you really want to print the former, you can insert the index instead of the child reference:

for pos, child in ipairs(workspace.Servers:GetChildren()) do
    if child:IsA("Folder") then
        table.insert(instances, pos)
    end
end

And you can unpack it

print(table.unpack(instances))
0
Omg... Thank you so much man you deserve respect and a good reputation. Thanks once again! TinfoilbotGamer 35 — 5y
Ad

Answer this question