Chunk System, random chunk issue

The codes creates a terrain based on chunks that are made from triangles(Wedges), the issue is that when I’m creating random special chunks it creates them in a diagonal line instead of creating them on random positions.

function Chunk.Event()
	local chance = 10  -- Define la probabilidad de un DarkChunk como 10%
	if math.random(100) <= chance then
		return "DarkChunk"
	else
		return "NormalChunk"
	end
end
function Chunk.new(chunkPosX, chunkPosZ)
	local chunkModel = Instance.new("Model")
	chunkModel.Name = "Chunk_" .. chunkPosX .. "_" .. chunkPosZ
	chunkModel.Parent = workspace  -- Asegúrate de asignar el parent adecuado para que sea visible y replicado
	local chunk = {
		instances = {};
		positionGrid = {};
		x = chunkPosX;
		z = chunkPosZ;
	}

	setmetatable(chunk, Chunk)

	local positionGrid = chunk.positionGrid
	local event = Chunk.Event()


	for x = 0, X do
		positionGrid[x] = {}

		for z = 0, Z do
			positionGrid[x][z] = getPosition(chunkPosX, chunkPosZ, x, z)
		end
	end

	for x = 0, X-1 do
		for z = 0, Z-1 do
			local a = positionGrid[x][z]
			local b = positionGrid[x+1][z]
			local c = positionGrid[x][z+1]
			local d = positionGrid[x+1][z+1]

			local wedgeA, wedgeB = draw3dTriangle(a, b, c)
			local wedgeC, wedgeD = draw3dTriangle(b, c, d)
			wedgeA.Parent = chunkModel
			wedgeB.Parent = chunkModel
			wedgeC.Parent = chunkModel
			wedgeD.Parent = chunkModel
            paintWedge(wedgeA, event)
			paintWedge(wedgeB, event)
			paintWedge(wedgeC, event)
			paintWedge(wedgeD, event)
			
			table.insert(chunk.instances, wedgeA)
			table.insert(chunk.instances, wedgeB)
			table.insert(chunk.instances, wedgeC)
			table.insert(chunk.instances, wedgeD)
		end
	end
addTrees(chunk, chunkModel, event)
	return chunk
end
1 Like

Can someone help me with the issue?