Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is ipairs in not working on an array?

Asked by 1 year ago
Edited 1 year ago

Doesn't print anything.

Script

for i,stat in ipairs(sinfo) do
    print(stat)
end

Array

{
    [1]={
        ["Name"] = "None",
        ["Gradient"] = ColorSequence.new({ColorSequenceKeypoint.new(0,Color3.new(0, 1, 0)),ColorSequenceKeypoint.new(1,Color3.new(0.607843, 0, 0))})
    },
    [2]={
        ["Name"] = "Money",
        ["Gradient"] = ColorSequence.new({ColorSequenceKeypoint.new(0,Color3.new(0, 1, 0)),ColorSequenceKeypoint.new(1,Color3.new(0, 0.607843, 0))}),
        ["Multi"] = {
            "None"
        },
        ["Icon"] = "8150337440"
    },
    [3]={
        ["Name"] = "Multiplier",
        ["Gradient"] = ColorSequence.new({ColorSequenceKeypoint.new(0,Color3.new(1, 0, 0)),ColorSequenceKeypoint.new(.5,Color3.new(0.588235, 0, 0)),ColorSequenceKeypoint.new(1,Color3.new(1, 0, 0))}),
        ["Multi"] = {
            ["Money"]=2.5
        },
        ["Icon"] = "8150337440"
    },
    [4]={
        ["Name"] = "Rebirth",
        ["Gradient"] = ColorSequence.new({ColorSequenceKeypoint.new(0,Color3.new(1, 0, 0)),ColorSequenceKeypoint.new(.5,Color3.new(0.588235, 0, 0)),ColorSequenceKeypoint.new(1,Color3.new(1, 0, 0))}),
        ["Multi"] = {
            "None"
        },
        ["Icon"] = "8150337440"
    },
}
0
Because you have a 2D table, you need to use nested loops. joshthegamer456 93 — 1y

1 answer

Log in to vote
0
Answered by 1 year ago

So what you are doing would only print the table of each dictionary. If you want it to print all the values of the tables within the tables then you would have to do this:

for table1, statTable in ipairs(sinfo) do
    for i, stat in pairs(statTable) do
        print(table1, i, stat)
    end
end

You should get something along the lines of:

1, Name, None
1, Gradient, ColorSequence.new({ColorSequenceKeypoint.new(0,Color3.new(0, 1, 0)),ColorSequenceKeypoint.new(1,Color3.new(0.607843, 0, 0))})

etc...
Ad

Answer this question