3d Array Sets Value on Table For All Children Instead of 1

What I am trying to accomplish / Background

I am trying to make a 3d array for saving cells (tables) with properties.
I only need positive layer and z coordinates so I have structured the cells [y][z][x].

Functions Used

These are the functions I am using, but I can’t seem to find any problem with them.

-- // Vars //

local CellTemplate = {
	Init = false,
	Platform = nil
}

local Local = {}

Local.Cells = {}

-- // Functions //

local function preGenCells()

	for layer=1, LevelConfig.Settings.Cell.VerticalLayers do
		print("Pregen: Layer >> "..layer)
		Local.Cells[layer] = {}
		for z=0, LevelConfig.Settings.Cell.Radius do
			Local.Cells[layer][z] = {}
			for x=-LevelConfig.Settings.Cell.Radius, LevelConfig.Settings.Cell.Radius do
				Local.Cells[layer][z][x] = CellTemplate
			end
		end
	end

end

function Shared:AllocateCell(layer: number, z: number, x: number, platformObj, overwrite: boolean)
	
	if Local.Cells[layer][z][x].Init and overwrite 
	then Local.Cells[layer][z][x].Platform:Destroy() end
	
	print("Allocated: "..x..", "..layer..", "..z)
	Local.Cells[layer][z][x].Init = true
	Local.Cells[layer][z][x].Platform = platformObj
	
end

-- Checks if the cell is available to be allocated

function Shared:CellAvailable(layer: number, z: number, x: number)
	
	if Local.Cells[layer] == nil then print("Layer Doesn't Exist") return false end
	if Local.Cells[layer][z] == nil then print("Z Doesn't Exist") return false end
	if Local.Cells[layer][z][x] == nil then print("X Doesn't Exist") return false end
	
	return not Local.Cells[layer][z][x].Init
	
end

Issue

After only running the function “AllocateCell” once it appears to affect every cell’s “Init”.
My intention is only to set “Init” to “true” under the cell I am trying to allocate.

Fixed, it was an issue with all cells referencing the “CellTemplate”.
Still getting used to Roblox’s referencing of stuff.

Fixed Code

-- Any nesting will require a change of table.clone() to table.deepCopy()

local CellTemplate = {
	Init = false,
	Platform = nil
}

local function preGenCells()

	for layer=1, LevelConfig.Settings.Cell.VerticalLayers do
		print("Pregen: Layer >> "..layer)
		Local.Cells[layer] = {}
		for z=0, LevelConfig.Settings.Cell.Radius do
			Local.Cells[layer][z] = {}
			for x=-LevelConfig.Settings.Cell.Radius, LevelConfig.Settings.Cell.Radius do
				Local.Cells[layer][z][x] = table.clone(CellTemplate)
			end
		end
	end

end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.