插入表格时出现了nil错误。

我在我的 Lua 代码中有以下功能。我得到一个错误Attempt to call field 'insert' (a nil value)

我在主 chunk 中有上面的代码

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

然后在功能中

function createDeck()
    cardDeck = {};
    for i=1, 4 do
        for j=1, 13 do
            local tempCard = suits[i]..j;--在这里打印显示有效值 i.e. "h1","d2" etc
                table.insert(cardDeck,tempCard); --这一行是错误的
        end
    end
end
点赞
用户2303714
用户2303714

你确定你的程序中没有其他地方定义了 table 变量吗?

> 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]: ?
2013-05-07 04:19:11