Help Creating a 3d Grid

I want to create a grid of 20x20x20 of attachments in a 200x200 cube, how would I do this in code? (I really just need help with the grid formula)

We can have a variable that will be the increment for each axis, as well as how many in each axis.
We’ll also have a starting point, which will determine where the first attachment will go, and it will continue from there.

local increment = 20 -- In studs
local amount = 20 -- Amount for each axis
local startingPoint = Vector3.new(0,0,0) -- In studs

Then, you can create a for loop that increments the x variable every time.

for x = 0, amount * increment, increment do
	
end

Then, inside that for loop, is another for loop for the y axis.

for x = 0, amount * increment, increment do
	for y = 0, amount * increment, increment do
		
	end
end

Then, inside that loop, is another loop for the z axis.

for x = 0, amount * increment, increment do
	for y = 0, amount * increment, increment do
		for z = 0, amount * increment, increment do
			
		end
	end
end

Inside that loop, we’ll create an attachment and set the position to the x, y, and z variables, subtracted by the startingPoint variable.

for x = 0, amount * increment, increment do
	for y = 0, amount * increment, increment do
		for z = 0, amount * increment, increment do
			local attachment = Instance.new("Attachment") -- If you plan on cloning attachments you can do so as well
			attachment.Position = Vector3.new(x, y, z) + startingPoint
		end
	end
end

Full code:

local increment = 20
local amount = 20
local startingPoint = Vector3.new(0,0,0)

for x = 0, amount * increment, increment do
	for y = 0, amount * increment, increment do
		for z = 0, amount * increment, increment do
			local attachment = Instance.new("Attachment")
			attachment.Position = Vector3.new(x, y, z) + startingPoint
		end
	end
end

Computationally, this is not efficient. With three for loops, it can slow down if overused. But this is a basic idea of how to position attachments in a 3D grid.

1 Like

Thank you!

its been a while since I’ve experimented with grids so thank you for refreshing my memory :slight_smile:

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