Say I have 20 bricks, all 10x10x5, and I'd want to arrange them in the game so that they're right next to each other, but not overlapping or leaving gaps, just on the "y" axis. That is, they'll all have the same "y" coordinate, but the x's and z's would be slightly randomized.
I could script the CFrame for each individual brick, but that would take way too long. Any ideas?
You can use two for
loops to move over the space in a grid.
Here's what we'll use to start:
for x = 0, 3 do for y = 0, 3 do print("(",x,",",y,")"); end end
Output:
( 0 , 0 ) ( 0 , 1 ) ( 0 , 2 ) ( 0 , 3 ) ( 1 , 0 ) ( 1 , 1 ) ( 1 , 2 ) ( 1 , 3 ) ( 2 , 0 ) ( 2 , 1 ) ( 2 , 2 ) ( 2 , 3 ) ( 3 , 0 ) ( 3 , 1 ) ( 3 , 2 ) ( 3 , 3 )
So this covered a grid of spaces.
Since the bricks are 10 wide in each way, their centers are 10 apart. That means we just need to multiply the grid we made by 10.
local parts = model:GetChildren() -- the parts we are placing local i = 0 for x = 1, 4 do for y = 1, 5 do i = i + 1; -- ith part parts[i].CFrame = CFrame.new( x * 10 , 0 , y * 10 ) end end