Hey! I want to make a for i=1 do loop, that creates a part, sets its position to the previous part’s (the part that the script created in the previous loop) position + Vector.new(5,0,0).
What im trying to achieve here is to create a line with parts, (which has spacings). Then, after a horizontal line is fully generated, i want the script to create an upper row starting from the same x position. I want this script to fully fill up a room with parts.
I tried to make this script:
local prevPart = nil
for i=1,50 do
local part = Instance.new("Part")
part.Parent = workspace
part.Anchored = true
part.CanCollide = false
if prevPart ~= nil then
part.Position = prevPart.Position + Vector3.new(5,0,0)
prevPart = part
else
part.Position = Vector3.new(0,0,0)
prevPart = part
end
end
However, this script is incomplete. As i said, i want for the script to also generate upper rows, but the script i wrote only generates the first row. Can you tell me if there is a short way to add upper rows to fill the room? Thanks for the help.
Exactly. I want it to make the bottom row, then go on and make the upper row, and repeat it until the room’s filled. by the way i will change the numbers until it fits my game, so feel free to generate as many rows as you like. i will change it anyways
-- How many rows you want on top
local layers = 5
-- How much gap between the layers
local gap = 5
local partsPerRow = 50
local function createPart()
local part = Instance.new("Part")
part.Parent = workspace
part.Anchored = true
part.CanCollide = false
return part
end
local function createRow(posY)
local previousPart = nil
for i=1, partsPerRow do
local part = createPart()
if previousPart ~= nil then
part.Position = previousPart.Position + Vector3.new(5, 0, 0)
previousPart = part
else
part.Position = Vector3.new(0, posY, 0)
previousPart = part
end
end
end
local posY = 0
for i=1, layers do
createRow(posY)
posY += gap
end
here i made one i didn’t test it tell me if it works btw
This is the “short way” to add upper rows: a nested loop.
If you truly want the first row to be driven by “previous part + Vector3.new(5,0,0)”, that’s just the inner loop above; the outer loop resets X and bumps Z by stepZ.
To stack vertically instead (make layers), add a third loop over Y and use origin.Y + layer * stepY.
-- CONFIG
local origin = Vector3.new(0, 0, 0) -- bottom-left corner of the grid (your start pos)
local cols = 50 -- how many across (X)
local rows = 20 -- how many up (Z)
local stepX = 5 -- spacing between parts along X
local stepZ = 5 -- spacing between rows along Z
local size = Vector3.new(4, 1, 4) -- size of each part
-- Optional: if you want to compute cols/rows from room size:
-- local roomSizeX, roomSizeZ = 250, 100
-- cols = math.floor(roomSizeX / stepX)
-- rows = math.floor(roomSizeZ / stepZ)
-- Make a template once (faster than Instance.new inside the loop)
local template = Instance.new("Part")
template.Size = size
template.Anchored = true
template.CanCollide = false
template.Material = Enum.Material.SmoothPlastic
template.Color = Color3.fromRGB(200, 200, 200)
for r = 0, rows - 1 do
for c = 0, cols - 1 do
local p = template:Clone()
-- Row r starts back at the same X as origin; only Z advances by r * stepZ
p.CFrame = CFrame.new(
origin.X + c * stepX,
origin.Y,
origin.Z + r * stepZ
)
p.Parent = workspace
end
end
template:Destroy()