我的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
答案 0 :(得分:4)
此错误通常来自于尝试将字段索引到非表格或nil的字段上。可能是错误发生时Account[i]
处的任何内容,不是表或用户数据,而是内置类型,如字符串或数字。
首先,当你收到错误时,检查Account[i]
中的任何内容的类型,并从那里开始。
看到此错误的两种最常见方式(我知道)如下:
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)