Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I arrange bricks?

Asked by
Zerio920 285 Moderation Voter
10 years ago

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?

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

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
0
this is interesting... what if the bricks aren't even each way? Say the bricks are 8 long and 10 wide. How would this change the script? Zerio920 285 — 10y
0
You would use * 8 for one coordinate, and * 10 for the other one. BlueTaslem 18071 — 10y
0
Ah ok, thank you :) Zerio920 285 — 10y
Ad

Answer this question