#Nested loops

46 messages · Page 1 of 1 (latest)

shut sinew
#

Hey can you help me understand what a nested loop is?

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(14.8, 0.5, 29.4)
    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```
native portal
#

loop inside of another loop is considered a nested loop

#

These are useful when we have 2 dimensional matrix data

#

basically imagine a list of folders inside of server storage and all of these folders have Instances inside of them

#

The nested loop would iterate through the folder's top level contents while the main loop would iterate through the folder in server storage

#

And regarding the code you sent, it would be cool if you could explain what you're trying to achieve

#

unless it's not really connected to the asked question

shut sinew
#

I'm trying to spawn parts on top of each other

native portal
#

first of all this one will fail horribly because you have your starting point and end point as heightIndex and TOWER_SIZE

#

but you don't compare it in any way <- ignore that
You don't change the index value in any way

#

secondly

shut sinew
#

hightIndex is 1 and TOWER_SIZE is 5 then it would count to 5 right?

native portal
#

read through this regarding for loops

#

to see the problem that you have

#

secondly why there's 2nd for loop for height?

shut sinew
#

i don't know that's why im asking

native portal
#

is that code written by you or?

#
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

-- Ignore this

shut sinew
native portal
#

it is listed there but it's listed as this:

for heightIndex = 1, TOWER_SIZE do

    local spawnY = (heightIndex - 1) * CUBE_SIZE

    currentColor = Color3.fromRGB(math.random(0, 255), math.random(0, 255), math.random(0, 255))

    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
#

Could you repeat what problem you're having?

shut sinew
#

I'm trying to understand what this means for example

#

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

#

I know it counts from 1 to 5

native portal
#

yes

shut sinew
#

but what does the next line mean

native portal
#

local spawnY = (heightIndex - 1) * CUBE_SIZE ?

shut sinew
#

yes

native portal
#

it assigns value to spawnY variable which gets calculated using heighIndex and CUBE_SIZE

#

basically for first iteration it would be local spawnY = (1 - 1) * 2

shut sinew
#

what is this for?

native portal
#

check for makecube function

shut sinew
#

ok I get 0 for spawnY what is next?

fossil flameBOT
#

studio** You are now Level 3! **studio

shut sinew
#

for spawnX I get 2

#

and spawnZ is 2

#

what does it do next? makecube(0, 2, 2)

#

nevermind I think that you multiple 2, 5 times since it counts to 5

#

I get 64 for spawnX

#

it shows this in output when I print spawnX

#

so for loop adds 2 each time it count from 1-5

#

spawnY is 0

#

spawnZ is 25

#

I got makecube(10,0,25)

#

What does it do next?