Issue with greedy mesher

Hello there. I’ve been working on extending my 1D greedy mesher to 3D, and for the most part it’s going great except I’ve been stuck on this one issue for quite a long time now. Quite often parts will overlap each other. Alot.

I’ve looked through literally every line of code, and I can’t seem to find what the issue is.

Here is the code:

local noiseSeed = math.random(-10e6, 10e6)
local gameSize = 64
local serverStorage = game:GetService("ServerStorage")
local textureObject = serverStorage.Texture

function allocate3D(width, height, depth, xOff, yOff, zOff, defaultValue)
	local s = os.clock()
	local array = table.create(width)
	
	for x = 1 + xOff, width + xOff do
		array[x] = table.create(height) -- Create a column
		for y = 1 + yOff, height + yOff do
			array[x][y] = table.create(depth) -- Create a layer
			for z = 1 + zOff, depth + zOff do
				array[x][y][z] = defaultValue -- Populate it!
			end
		end
	end
	print(os.clock() - s)
	return array
end

function generateChunk(xOff, yOff, zOff)
	local allocatedTable = allocate3D(gameSize, gameSize, gameSize, xOff, yOff, zOff)
	for x = 1, gameSize do
		for y = 1, gameSize do
			for z = 1, gameSize do
				local noiseDensity = math.noise(((x + noiseSeed + xOff) / 32), (y + noiseSeed + yOff) / 32, (z + noiseSeed + zOff) / 32)-- - ((y + yOff - 64) / 16)
				if noiseDensity > 0 then
					allocatedTable[x + xOff][y + yOff][z + zOff] = 1
				else					
					allocatedTable[x + xOff][y + yOff][z + zOff] = 0
				end
				--allocatedTable[x + xOff][y + yOff][z + zOff] = 1
			end
		end
		wait()
	end
	
	return allocatedTable
end

local freeAllocatedTable = allocate3D(gameSize, gameSize, gameSize, 0, 0, 0)
local mainTerrainData = generateChunk(0, 0, 0)
local xEdge1 = generateChunk(gameSize, 0, 0)
local xEdge2 = generateChunk(-gameSize, 0, 0)
local yEdge1 = generateChunk(0, gameSize, 0)
local yEdge2 = generateChunk(0, -gameSize, 0)
local zEdge1 = generateChunk(0, 0, gameSize)
local zEdge2 = generateChunk(0, 0, -gameSize)
local blackList = freeAllocatedTable

local vec3_1 = Vector3.new(1, 1, 1)
local vec3_05 = Vector3.new(0.5, 0.5, 0.5)

function superCompare(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
	return a == h and b == i and c == j and d == k and e == l and f == m and g == n
end

function greedymeshVoxel(voxelIndex, mat, cullData)
	if blackList[voxelIndex.X][voxelIndex.Y][voxelIndex.Z] then
		return
	end
	local startVecOfVoxel = Vector3.new(voxelIndex.X, voxelIndex.Y, voxelIndex.Z)
	local xExtension = 1
	for x = voxelIndex.X + 1, gameSize do
		if blackList[x][voxelIndex.Y][voxelIndex.Z] then
			break
		end
		local notAir = mainTerrainData[x][voxelIndex.Y][voxelIndex.Z]
		
		if notAir == mat then
			xExtension += 1
		else
			break
		end
	end
	local zExtension = 1
	
	for z = voxelIndex.Z + 1, gameSize do
		local breakLoop = false
		for x = voxelIndex.X + 1, xExtension do
			if blackList[x][voxelIndex.Y][z] then
				breakLoop = true
				break
			end
			local notAir = mainTerrainData[x][voxelIndex.Y][z] 
			if notAir == mat then
				if x == xExtension then
					zExtension += 1
				end
			else
				breakLoop = true
				break
			end
		end
		if breakLoop then
			break
		end
	end
	
	local yExtension = 1
	
	for y = voxelIndex.Y + 1, gameSize do
		local breakLoop1 = false
		for z = voxelIndex.Z + 1, zExtension do
			local breakLoop2 = false
			for x = voxelIndex.X + 1, xExtension do
				if blackList[x][y][z] then
					breakLoop2 = true
					break
				end
				local notAir = mainTerrainData[x][y][z]
				
			if notAir == mat then
					if x == xExtension then
					end
				else
					breakLoop2 = true
					break
				end
			end
			if z == zExtension and not breakLoop2 then
				yExtension += 1
			end
			breakLoop1 = breakLoop2
			if breakLoop2 then
				break
			end
		end
		if breakLoop1 then
			break
		end
	end
	
	local size = Vector3.new(xExtension, yExtension, zExtension)-- + vec3_1
	local center = startVecOfVoxel + size
	
	for x = voxelIndex.X, xExtension do
		for y = voxelIndex.Y, yExtension do
			for z = voxelIndex.Z, zExtension do
				blackList[x][y][z] = true
			end
		end
	end
	
	return {
		["Size"] = size;
		["Start"] = startVecOfVoxel;
		["Center"] = startVecOfVoxel + size * 0.5 - vec3_05;
	}
end

local cuboids = {}

for x = 1, gameSize do
	wait()
	for y = 1, gameSize do
		for z = 1, gameSize do
			local terrainGenCell = mainTerrainData[x][y][z]
			if terrainGenCell == 1 and not blackList[x][y][z] then
				--local genCell, visSurfaces = cull(x, y, z)
				
				--if genCell then
					local cuboid = greedymeshVoxel({
						X = x,
						Y = y,
						Z = z
					}, terrainGenCell)
					blackList[x][y][z] = true
					if cuboid then
						table.insert(cuboids, cuboid)
					end
				--end
			end
		end
	end
end

for _, cuboid in pairs(cuboids) do
	wait()
	local block = Instance.new("Part")
	block.Anchored = true
	block.Size = cuboid.Size * 4
	block.CFrame = CFrame.new(cuboid.Center * 4)
	block.Parent = workspace
	block.Transparency = 0
	--break
end

Is there anything I can do to fix this, and is there anything that I’m not doing correctly?