Confusion of Duplication

I put your code under eachother in order of execution, to try to understand it. But some parts don’t add up for me and might be the cause for your problem. (i’ve added ? where that happens)

-- here you create the array
local array = createArray(15,15,0)
-- it is a 2d jagged array where every value is 0
-- example:
[
    [0,0,0,0],
    [0,0,0,0],
    [0,0,0,0],
    [0,0,0,0]
]
-- every array is a row and every 0 is a column

-- now you use this array to create breakables and unbreakables
function breakables(array)
    -- j is the index of the row
    -- v is the actual array on that row
	for j, v in pairs(array) do
        -- half of the rows will become unbreakable
		if j % 2 == 1 then
			tileType:Unbreakable(array,j) -- ? array is the entire map, j is the current row

        -- every other row has a 75% of being breakable, otherwise staying empty
		elseif math.random() > .25 then
			tileType:Breakable(array,j) -- ? array is the entire map, j is the current row
		end
	end
end
function tileType:Unbreakable(array,num)
    -- tabBlocks is an empty table/array
	if tabBlocks[num] then -- if there is a value stored at the current row in tabBlocks
		local pos = tabBlocks[num]
		local block= unBreakable:Clone() -- ? then create a single unbreakable for the entire row
		block.Parent = fileBlocks:WaitForChild("Unbreakables")
		block.Position = pos + Vector3.new(0,4,0)
	end

    -- i is the index of the row
    -- v is the actual array on that row
	for i, v in pairs(array) do
        -- ?(_ should only be used when you don't use the variable)
        -- _ is the index of the column in v
        -- j is the value of the column in v (0 by default)
		for _, j in pairs(v) do
            -- ? if there is a value at the index of the column divided by 2 in tabBlocks
			if tabBlocks[_/2] then
				local pos = tabBlocks[num] -- read the value at the current row in tabBlocks
				local block= unBreakable:Clone() -- then create an unbreakable
				block.Parent = fileBlocks:WaitForChild("Unbreakables")
				block.Position = pos + Vector3.new(4*_,4,0) -- with position being the index of the column in v multiplied by 4 for the x axis
                -- ?(but the z axis is always 0, so blocks can only be next to eachother, not above eachother)
			end
		end
	end
end

tabBlocks is read using both the column index/2 and the row index, I don’t know what it’s supposed to do.
Also as far as i can tell values in array are never changed nor values in tabBlocks.
So the 2 times that you do local pos = tabBlocks[num] pos is always 0 or nil

Doesn’t work.

this is the code

local tileType = require(script.Parent:WaitForChild("TileType"))

function createArray(rows, cols, defualtVal)
	local arr = {}
	
	for x = 1, rows do
		arr[x] = {}
		for y = 1, cols do
			arr[x..':'..y] = defualtVal
		end
	end
	
	return arr
end

function breakables(array)
	for j, v in pairs(array) do
		if j % 2 == 1 then
			tileType:Unbreakable(array)
		elseif math.random() > .25 then
			tileType:Breakable(array)
		end
	end
end

local array = createArray(15,15,0)

tileType:AirType(array)
breakables(array)
local tileType = {}

local blocks = script:WaitForChild("Blocks")
local defaultBlock = blocks:WaitForChild("Default")
local breakableBlock = blocks:WaitForChild("Breakable")
local unBreakable = blocks:WaitForChild("Unbreakable")

local dunderman = workspace:WaitForChild("Dunderman")
local files = dunderman:WaitForChild("Files")
local fileBlocks = files:WaitForChild("Blocks")

local tabBlocks = {}

function tileType:AirType(array)
	for PositionKey, Value in pairs(array) do
		local PosTable = string.split(PositionKey,':')
		local X = PosTable[0]
		local Y = PosTable[1]
		
		print('X'..X,"Y"..Y,"Val"..Value)

		local block = defaultBlock:Clone()
		block.Position = Vector3.new(X,0,Y)
		block.Parent = fileBlocks.Tiles
	end
end

function tileType:Unbreakable(array,num)
	for PositionKey, Value in pairs(array) do
		local PosTable = string.split(PositionKey,":")
		local X = PosTable[1]
		local Y = PosTable[2]
		
		local block = unBreakable:Clone()
		block.Position = Vector3.new(X,4,Y)
		block.Parent = fileBlocks.Unbreakables
	end
end

Keeps on saying this - ServerScriptService.TileType:20: attempt to concatenate string with nil

Can you try helping me with this?

Arrays in lua start with 1, so it should be

local X = PosTable[1]
local Y = PosTable[2]

But i don’t recommend doing

array["10:1"]
-- instead of
array[10][1]

because array[10][1] is most definitly faster
(and is less characters to write)

The issue of it destroying all objects in your original post is because of this you comparing the same parts to each when the first loops V matches the 2nd loop of same childrens V2 which is the same part it removes it you would need to check by name or something not by the instance

Also on the above post about changing your arrays over like I suggested you don’t have to its just one way I have setup row cell systems like this before to visualize it easier, Sorry that was a quick post did last night

The way you have it is fine just make sure you don’t double loop here is the the 2 functions and how they would read through your original

local tileType = {}

local blocks = script:WaitForChild("Blocks")
local defaultBlock = blocks:WaitForChild("Default")
local breakableBlock = blocks:WaitForChild("Breakable")
local unBreakable = blocks:WaitForChild("Unbreakable")

local dunderman = workspace:WaitForChild("Dunderman")
local files = dunderman:WaitForChild("Files")
local fileBlocks = files:WaitForChild("Blocks")

local tabBlocks = {}

function tileType:AirType(array)
	for x, rowdata in ipairs(array) do
		for y, value in ipairs(rowdata) do
			task.spawn(function()  -- this will make it almost instantly setup all blocks if it lags remove this
				local block = defaultBlock:Clone()
				block.Position = Vector3.new(x,0,y)
				block.Parent = fileBlocks.Tiles
			end)
		end	
	end
end

function tileType:Unbreakable(array,num)
	for x, rowdata in ipairs(array) do
		for y, value in ipairs(rowdata) do
			task.spawn(function()     -- this will make it almost instantly setup all blocks if it lags remove this
				local block = unBreakable:Clone()
				block.Position = Vector3.new(x,4,y)
				block.Parent = fileBlocks.Unbreakables
			end)
		end	
	end
end
1 Like
local X = PosTable[1]
local Y = PosTable[2]

This didn’t work. Any other way that you can help me. (without changing the entire code)

What about it isn’t working?
Maybe try printing the PositionKey before you do string.split().

This is what it printed.

1  -  Server - TileType:16

PositionKey doesn’t have a : in it so y is nil

local PositionKey = "1"
local PosTable = string.split(PositionKey,':')
local X = PosTable[1] -- 1
local Y = PosTable[2] -- nil

Edit:

function createArray(rows, cols, defualtVal)
	local arr = {}
	
	for x = 1, rows do
		arr[x] = {} -- i think you have to remove this line
		for y = 1, cols do
			arr[x..':'..y] = defualtVal
		end
	end
	
	return arr
end