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

Creating a 3D array with code, how do I do it?

Asked by
ausmel105 140
8 years ago

Hi everyone,

I know how I would set this out if I could set the data structure out beforehand, by doing:

local array = { foldername = {child, child}; foldername = {child, child} }

But how do I do this when I just can't foresee how many folders (or any other object for that matter) and need a variable amount of secondary arrays?

I tried this, but it errors.

local array = {}

for i, v in pairs(sp:GetChildren()) do
    if v:IsA("Folder") then
        table.insert(array, v)
        for _, x in pairs(v:GetChildren()) do
            table.insert(array[i], x.Name)
        end
    end
end

My output:

14:30:01.904 - Workspace.Script:5: bad argument #1 to 'insert' (table expected, got Object)

14:30:01.905 - Script 'Workspace.Script', Line 5

14:30:01.906 - Stack End

1 answer

Log in to vote
2
Answered by 8 years ago

Just remember to set the key you're accessing to a table.

local array = {}
for i, v in pairs(sp:GetChildren()) do
    if v:IsA("Folder") then
        array[i] = {}; -- Set it to an array so that there's actually room there
        for n, x in pairs(v:GetChildren()) do
            array[i][n] = x.Name; -- I'd advise against table.insert when alternatives exist, except for in making cleaner code.
        end
    end
end
0
Thanks! That seems to work. ausmel105 140 — 8y
Ad

Answer this question