Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table insert gives a nil error

Tags:

lua

lua-table

I have the following function in my lua code. I am getting an error Attempt to call field 'insert' (a nil value)

I have above in main chunck

local cardDeck;
local suits = {"h","d","c","s"};

then in function

function createDeck()
    cardDeck = {};
    for i=1, 4 do
        for j=1, 13 do
            local tempCard = suits[i]..j;--PRINT SHOW VALID VALUE HERE i.e. "h1","d2" etc
                table.insert(cardDeck,tempCard); --THIS LINE IS ERROR
        end
    end
end
like image 506
james Avatar asked May 13 '26 19:05

james


1 Answers

Are you sure you haven't defined table elsewhere in your program?

> table.insert({},1)
> table = {}
> table.insert({},1)
stdin:1: attempt to call field 'insert' (a nil value)
stack traceback:
    stdin:1: in main chunk
    [C]: ?
like image 53
HennyH Avatar answered May 17 '26 12:05

HennyH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!