3

Lua コードで次のエラーが発生します。

フィールド '?' にインデックスを付けようとしています (ゼロ値)

以下の太字の行で発生します。どうすれば修正できますか?

function SendMessageToAdmins(color1, color2, color3, msg)
    for i = 0, maxSlots - 1 do
        if Account[i] and Account[i].Admin >= 1 or Account[i] and Account[i].GameMaster >= 1 then
            SendPlayerMessage(i, color1, color2, color3, string.format("%s", msg))
        end
    end
end
4

1 に答える 1

7

このエラーは通常、テーブルまたは nil ではないもののフィールドにインデックスを付けようとした場合に発生します。Account[i]エラーが発生したときに、テーブルやユーザーデータではなく、文字列や数値などの組み込み型である可能性があります。

Account[i]エラーが発生したときに何が入っているかを確認し、そこから始めます。

このエラーを表示する最も一般的な 2 つの方法 (私が知っている) は次のとおりです。

local t = { [1] = {a = 1, b = 2}, [2] = {c = 3, d = 4} }
-- t[5] is nil, so this ends up looking like nil.a which is invalid
-- this doesn't look like your case, since you check for 
-- truthiness in Account[i]
print(t[5].a)

おそらくあなたが経験しているケースは、おそらくこれです:

local t =
{
    [1] = {a = 1, b = 2},
    [2] = 15, -- oops! this shouldn't be here!
    [3] = {a = 3, b = 4},
}
-- here you expect all the tables in t to be in a consistent format.
-- trying to reference field a on an int doesn't make sense.
print(t[2].a)
于 2012-08-28T13:49:45.047 に答える