Why does my script not create a grid the way I want it to?

Hey there!

I’ve been trying to create a system to make a grid of blocks, but it’s not generating it how I would like it to. Of course I want it to be a grid, but it just makes this:
https://streamable.com/c0pi7m

Here is the code I am using to make the grid:

local piece
local lastPiece
local newPiece
local spacingSpace = CFrame.new(6, 0, 0)
local spacingRow = CFrame.new(0, 0, 6)

local onRow = 1
local onSpace = 1

local spaceMax = 10
local rowMax = 4

local function CreateGrid()
	for i = 1,spaceMax do
		wait(.1)
		if not newPiece then
			piece = game.ReplicatedStorage:WaitForChild("GridPiece"):Clone()
		else
			piece = newPiece:Clone()
		end
		if lastPiece and onRow == 1 then
			piece.Main.CFrame = lastPiece.Main.CFrame * spacingSpace
			piece.Border.CFrame = lastPiece.Border.CFrame * spacingSpace
			piece.Bottom.CFrame = lastPiece.Bottom.CFrame * spacingSpace
			piece.GridHumanoidPosition.CFrame = lastPiece.GridHumanoidPosition.CFrame * spacingSpace
		end
		piece.Name = "GridPieceR"..tostring(onRow).."S"..tostring(onSpace)
		lastPiece = piece
		piece.Parent = workspace:WaitForChild("GridStorage")
		
		onSpace = onSpace + 1
		
		if onSpace == spaceMax and onRow < rowMax then
			onRow = onRow + 1
			onSpace = 1
			
			newPiece = game.ReplicatedStorage:WaitForChild("GridPiece"):Clone()
			newPiece.Main.CFrame = workspace.GridStorage["GridPieceR"..tostring(onRow-1).."S"..tostring(onSpace)].Main.CFrame * spacingRow
			newPiece.Border.CFrame = workspace.GridStorage["GridPieceR"..tostring(onRow-1).."S"..tostring(onSpace)].Border.CFrame * spacingRow
			newPiece.Bottom.CFrame = workspace.GridStorage["GridPieceR"..tostring(onRow-1).."S"..tostring(onSpace)].Bottom.CFrame * spacingRow
			newPiece.GridHumanoidPosition.CFrame = workspace.GridStorage["GridPieceR"..tostring(onRow-1).."S"..tostring(onSpace)].GridHumanoidPosition.CFrame * spacingRow
			newPiece.Parent = game.ReplicatedStorage
			CreateGrid()
			
			lastPiece = nil
		end
	end
end

CreateGrid()

Any help is greatly appreciated!

When you use the word “space” in your code, is that equivalent to the word “column”?
A lot of times people will use “row” and “column” when referring to positions on a grid.

A lot of times, code clarity can help with seeing the problem.

I could rewrite the general layout of your code like this:

local function CreateGrid()
	for gridY = 1, gridHeight do -- loop over each column (space)

		wait(.1)

		for gridX = 1, gridWidth do -- loop over each row

			print(gridX,gridY) -- should print out like (1,1) (2,1) (3,1) ... etc

			-- create each new grid part here

			if gridX == gridWidth and gridY == gridHeight then
				-- runs only for the very like grid tile
			end

		end
	end
end

This isn’t the entire code, just a different way of writing the loop.

1 Like

In the video the last grid piece placed in row 1, is it creating multiple grid pieces at that same spot?

Easy code to make a grid of parts:

local baseplate = workspace.Baseplate;
local origin = Vector3.new(0,0,0);
local cellcount_x = 10;
local cellcount_z = 10;
local cellsize_x = 10;
local cellsize_y = 1;
local cellsize_z = 10;

for x = 0, cellcount_x do
	for z = 0, cellcount_z do
		local newPart = Instance.new("Part");
		newPart.Size = Vector3.new(cellsize_x,cellsize_y,cellsize_z);
		newPart.Position = Vector3.new(origin.X + (x * cellsize_x), origin.Y, origin.Z + (z * cellsize_z));
		newPart.Anchored = true;
		newPart.Name = "row("..x.."):col("..z..")";
		newPart.Parent = workspace;
		newPart.Color = Color3.new(x/cellcount_x,cellsize_y,z/cellcount_z);
	end
end

I’m going to see if this one works

I see you marked it as solved. But I hope there weren’t any other issues. Because my code example left out a lot of your code.

Working the code into the script wasn’t all too hard, just the spacing in the grid was wrong. This was an easy fix by doing this:

for gridY = 1, gridHeight, spacing do

end
1 Like

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