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

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

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

✓ Accepted answer

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

1 comment

0

Thanks! That seems to work.

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