#Looping help needed

1 messages · Page 1 of 1 (latest)

near dove
#

local script

local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local Module = require(ReplicatedStorage.Module)
local Info = require(ReplicatedStorage.Module.Info)
local Grid = script.Parent

Module.Create(Grid, Info.Stage1)

main module

local Module = {}

function Module.Create(Grid, Info)
    local BlockTemplate = script.Block
    local PathTemplate = script.Path
    local TeleporterTemplate = script.Teleporter

    (logic here)
end

return Module

info module

local Info = {
    Stage1 = {
        Blocks = {5},
        Paths = {1, 2, 3, 4, 6, 7, 8},
        Teleporters = {9}
    }
}

desired behavior in the main module: goes through the info of stage1 in numerical order and clones its respective template and parents it to the grid, name it its respective number for example, it would clone paths 1-4 using the path template and then it would clone a teleporter (5) and repeat until 9

dull vigil
#
local Info = {
    {
        Name = "Stage1",
        Blocks = {5},
        Paths = {1, 2, 3, 4, 6, 7, 8},
        Teleporters = {9}
    },{
        Name = "Stage2",
        Blocks = {5},
        Paths = {1, 2, 3, 4, 6, 7, 8},
        Teleporters = {9}
    },{
        Name = "Stage3",
        Blocks = {5},
        Paths = {1, 2, 3, 4, 6, 7, 8},
        Teleporters = {9}
    }
}

for index, data in info do
    print("Stage" .. index) -- dynamic name generation using the index
    print("Name" .. data.Name)
    for dataIndex, Value in data do
        print(dataIndex, Value)
    end
end
near dove
dull vigil
near dove
dull vigil
#

If you want to loop through them in order

near dove
dull vigil
#

If you don't need to loop them in order then you can keep it like the original

near dove
#

because my system would generate one stage at a time so it only requires the respective stage like stage1

#

is the example you provided able to loop through the content of a certain stage?

dull vigil
#

I think a number index is more meaningful then a string index like stage1

#

And is a lot easier to write code the iterate the stages

#

It also lets you get the length on the array

#

But at the end of the day only your can decide depending on the design of your game

#

Another option is to have both

near dove
#

@dull vigil is there a way to display info with the stage name as the table and not as “Name”

dull vigil
#

?

near dove
dull vigil