#Nested loops

2 messages · Page 1 of 1 (latest)

glacial grotto
#

Hey I'm learning nested loops. Can you help me understand why do I need X, Y and Z instead of just Y. Parts spawn on each other.

local CUBE_SIZE = 2

function makecube()
    local cube = Instance.new("Part")
    cube.Size = Vector3.new(CUBE_SIZE, CUBE_SIZE, CUBE_SIZE)
    cube.Color = Color3.fromRGB(255, 255, 0)
    cube.CFrame = CFrame.new()
    cube.Parent = game.Workspace
end

for heightIndex = 1, TOWER_SIZE do
    local spawnY = (heightIndex - 1) * CUBE_SIZE
    for heightIndex = 1, TOWER_SIZE do
        local spawnY = (heightIndex - 1) * CUBE_SIZE

        for lengthIndex = 1, TOWER_SIZE do
            local spawnX = lengthIndex * CUBE_SIZE
            
            for widthIndex = 1, TOWER_SIZE do
                local spawnZ = widthIndex * CUBE_SIZE
                makecube(spawnX, spawnY, spawnZ)
                task.wait(0.25)
            end
            end
        end
end```
onyx epoch
#

local TOWER_SIZE = 5
local CUBE_SIZE = 2

function makecube(x, y, z)
local cube = Instance.new("Part")
cube.Size = Vector3.new(CUBE_SIZE, CUBE_SIZE, CUBE_SIZE)
cube.Color = Color3.fromRGB(255, 255, 0)
cube.CFrame = CFrame.new(x, y, z) -- Use the x, y, z coordinates to position the cube
cube.Parent = game.Workspace
end

for yIndex = 1, TOWER_SIZE do
local spawnY = (yIndex - 1) * CUBE_SIZE -- Calculate the Y position
for xIndex = 1, TOWER_SIZE do
local spawnX = (xIndex - 1) * CUBE_SIZE -- Calculate the X position
for zIndex = 1, TOWER_SIZE do
local spawnZ = (zIndex - 1) * CUBE_SIZE -- Calculate the Z position
makecube(spawnX, spawnY, spawnZ) -- Create the cube at the calculated position
task.wait(0.25)
end
end
end