i am making a game where you jump from tile to tile doging crazy events but i just dont know how to generate parts in a grid like pattern
Use a double nested for loop. Iβm rusty in lua (c++ instead), but Iβll give a pseudocode sample.
- Define numColumns, numRows.
- Define spacing gap (in studs)
- Outer for loop for row:
- Define xOffset using your current index (i * gapSize)
- Inner for loop for columns:
- Define yOffset using your current index (i * gapSize)
- Create your part, then move/offset it based on your x and y values.
What you end up with is a grid. Choosing different values for # iterations for x and y will turn it into a rectangle.
Translated to lua:
local maxX=4
local maxY=4
local gap=5
for x=1,maxX do
for y=1,maxY do
local newPart=Instance.new('Part',workspace) or `YourPartHere`
newPart.Position=Vector3.new(x,0,y)*gap
end end
Please reply with any issues or concerns
Looks like it should work. Thanks for helping here.
this would work but
im really nitpicky, but the organization! it doesnt matter for functionality but it just looks nice, plus it can make it easier for ppl to understand the code. also, you cant define a center position for the squares to generate at and you cant set the square size (ik OP didnt specify this but i think he would want it). also, i think the OP should know that we are changing the Z axis for the rows, not the Y axis. heres a refined version
local origin = Vector3.new(0, 10, 0) -- the point which the tiles should generate around
local tileSize = Vector3.new(5, 1, 5) -- size of each tile
local numRows = 4
local numColumns = 4
local spacing = 5 -- # of studs between each tile
--[[
numRows would be the z axis (not y because y is up and down in 3D) and numColumns would be the x axis
columns
β--β β--β β--β β--β
β ββββ ββββ ββββ ββββ-β
β ββββ ββββ ββββ ββββ-β
β ββββ ββββ ββββ ββββ-β
z ββββ ββββ ββββ ββββ-β rows
β ββββ ββββ ββββ ββββ-β
β ββββ ββββ ββββ ββββ-β
β ββββ ββββ ββββ ββββ-β
β ββββ ββββ ββββ ββββ-β
<--------- x --------->
]]
-- calculate total grid size
local totalSizeX = numColumns * tileSize.X + (numColumns - 1) * spacing
local totalSizeZ = numRows * tileSize.Z + (numRows - 1) * spacing
-- calculate the position of the top-left tile so the grid is centered around the origin
local startPosition = origin - Vector3.new(totalSizeX/2 - tileSize.X/2, 0, totalSizeZ/2 - tileSize.Z/2)
for xPosition = 0, numColumns - 1 do
for zPosition = 0, numRows - 1 do
local tile = Instance.new("Part")
tile.Size = tileSize
tile.Anchored = true
tile.Material = Enum.Material.Neon
tile.BrickColor = BrickColor.new("Bright red")
tile.Position = (
startPosition +
Vector3.new(
xPosition * (tileSize.X + spacing),
0,
zPosition * (tileSize.Z + spacing)
)
)
tile.Parent = workspace
end
end
phew
that ascii diagram took way too long
Youβre totally right! I was just being lazy :p Thank you so much for improving my code
thanks for that been trying to figure that out for days
no problem. also please make sure you read over the code and comments and do your best to understand it - this is extremely important to improve as a developer. if you have any questions, let me know
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
