Print(table[#]) prints entire table instead of specified value

i want to read from an array, but when i try to, it prints the entire array. also trying to index 2 or 3 returns nil

ive searched this up many times, rewording the sentence in many ways while doing so, and couldn’t find a solution. only found posts where the problem was caused by things like putting letters inside of [index] and such.

--the print statement
print(gmltmdata[1])

--the output
                         ▼  { 
                    [1] = 1,
                    [2] = 0.501960813999176,
                    [3] = 0.501960813999176
                 }


--the print statement
print(gmltmdata[2])

--the output
nil


--the print statement
print(gmltmdata[3])

--the output
nil

(sorry if the solution turns out to be something dumb.)

Is it possible that above this code you declared gmltmdata with more than one bracket?

gmltmdata = []

or

gmltmdata = {}

If not could you please reveal more of the code?

You sure you didn’t nest your table by 1 more accidentally?

1 Like

ok so, gmltmdata is actually an array, that contains the table you wanted like this:

local gmltmdata = {
   [1] = { 1, 0.501, 0.501 }
}

there’s no index 2, 3, 4, 5, and so on because it is inside another table that only contains the table you wanted which is index 1.
so, redefine the gmltmdata table:

gmltmdata = gmltmdata[1]
print(gmltmdata[1]) -- 1

or, just remove the outter brackets:

local gmltmdata = { 1, 0.501, 0.501 }
print(gmltmdata[1]) -- 1
2 Likes

From your results it seems like your table is implemented like this:

local gmltdata = {{
                    [1] = 1,
                    [2] = 0.501960813999176,
                    [3] = 0.501960813999176
}}

Did you initialise the table yourself or is it returned from somewhere? Please reveal more code so we can figure out.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.