For the entire post i'm going to be calling the tables arrays since they are practically the same thing in my eyes.
I'm making a 3D array for a block-based terrain generator that stores all the blocks that have been generated, with the X value length being the world length and the Y value length being the height of the world. The Z axis is for the foreground block and background block, so it's only 2 blocks.
The main variable to focus on is the worldBlocks array, and the blocks array just stores block ID's that I have corresponding blocks to in the server storage.
Here's the code snippet that creates the 3D array:
for i = 1, worldLength do
local newArray = {}
worldBlocks[i] = newArray --adds an array to each value
for o = 1, maximumHeight do
local newerArray = {}
worldBlocks[i][o] = newerArray --adds an array to each array
end
end
The next code is my clone block function that clones a block and stores it inside the 3D table, but this is where the error is coming from.
local function cloneBlock(X,Y,Z,BlockID)
local nB = blocks[BlockID]:Clone()
nB.Position = Vector3.new(X*blockSize,Y*blockSize,Z*blockSize)
nB.Parent = game.Workspace
if (worldBlocks[X][Y][Z])then --The error, this line is supposed to determine whether or not there is already a block here.
worldBlocks[X][Y][Z]:Destroy()
worldBlocks[X][Y][Z] = nB
else --if not just set the position in the array to the new block
worldBlocks[X][Y][Z] = nB
end
end
At first I believed it was because there was no previous value assigned to it, but after multiple tries i've confirmed, at least in my eyes, that making a template block for each space did not help solve the solution. In that case, i'm not entirely sure what solutions are available. It's definetly some form of an out of bounds error, but after work with 2D arrays those rarely errored for me in that regard. I'd love to get insight on how these things work in lua, and if there's a more efficient method please tell.
SOLVED! It was merely a matter of me not understanding what the error is trying to convey. My code kept attempting to call cloneBlock while passing decimal numbers into it, which is a horrible idea for storing values in arrays. Therefore, my final code addresses it by using math.floor on X,Y, and Z before the rest of the code is ran inside the function.

2 comments
first of all, the new array variables are unnecessarily elongating your code, you can just set the table value to an empty table
Alright will do that, should help clean it up