Cannot compare numbers in a Table

Hi guys!

When I was inserting strings in a table following worked like a charm, but when I insert numbers in form of array I get a: "attempt to index field ‘?’ (a number value) stack traceback:…"

local myButtons = { 3, 1, 2} function checkButtonsValue()     for i = 1, #myButtons do          if (myButtons[i] == 1) then          print("ONE")                elseif (myButtons[i] == 2) then         print("TWO")      end    end

Why I cannot compare what number is in my table like this?

Basically I am trying to make a number array inside a table, a check its order/suffle it, and etc…

Merry Christmas and all the Best for incoming holidays!

Goran

There’s nothing inherently wrong with your code, other than the fact that you’ve missed out an “end” but I assume that’s just a typo.

I’ve made a simplified checkButtonsValue, so you can see that it is printing as expected:

local myButtons = { 3, 1, 2} function checkButtonsValue() for i = 1, #myButtons do print("when i = "..i..", value = "..myButtons[i]) end end checkButtonsValue()

The output from this is as expected:

when i = 1, value = 3 when i = 2, value = 1 when i = 3, value = 2

My best guess for why you are getting errors is that you are trying to change the table while it is being looped through, and this is breaking the for loop. Without seeing more of your code, it’s hard to say exactly how this is occurring.

A common way to break loop is to remove an entry from a table while in the middle of a loop:

for i = 1, #myButtons do if myButtons[i] == 3 then table.remove(myButtons, i) end myButtons[i] = myButtons[i] \* 2 end

The problem here is that when the loop starts, myButtons has 3 entries. However when i is 1 (and myButtons[i] == 3) I remove an entry from the table, which means there are now only 2 entries in the table, and myButtons[3] is now nil.

However the loop had 3 passed in as the number of times to loop, so an error will occur when i == 3 because it will attempt to multiply nil * 2.

Edit: I forgot to give a solution if you are removing entries during a loop, which is to loop backwards:

for i = #myButtons, 1, -1 do

Thanks Alan!
You’ve been very helpful.

Goran

There’s nothing inherently wrong with your code, other than the fact that you’ve missed out an “end” but I assume that’s just a typo.

I’ve made a simplified checkButtonsValue, so you can see that it is printing as expected:

local myButtons = { 3, 1, 2} function checkButtonsValue() for i = 1, #myButtons do print("when i = "..i..", value = "..myButtons[i]) end end checkButtonsValue()

The output from this is as expected:

when i = 1, value = 3 when i = 2, value = 1 when i = 3, value = 2

My best guess for why you are getting errors is that you are trying to change the table while it is being looped through, and this is breaking the for loop. Without seeing more of your code, it’s hard to say exactly how this is occurring.

A common way to break loop is to remove an entry from a table while in the middle of a loop:

for i = 1, #myButtons do if myButtons[i] == 3 then table.remove(myButtons, i) end myButtons[i] = myButtons[i] \* 2 end

The problem here is that when the loop starts, myButtons has 3 entries. However when i is 1 (and myButtons[i] == 3) I remove an entry from the table, which means there are now only 2 entries in the table, and myButtons[3] is now nil.

However the loop had 3 passed in as the number of times to loop, so an error will occur when i == 3 because it will attempt to multiply nil * 2.

Edit: I forgot to give a solution if you are removing entries during a loop, which is to loop backwards:

for i = #myButtons, 1, -1 do

Thanks Alan!
You’ve been very helpful.

Goran