尝试在FiveM essentialmode上对一个空值(字段'?')进行索引

我在代码的某个部分遇到了问题,我不是程序员,我在互联网上查找过,但没有找到任何能够帮助我的问题。该问题出现在第234行,即if groups[Users[source].getGroup()]:canTarget(group) then我不知道该怎么办,这是错误

Error running call reference function for resource essentialmode: citizen:/scripting/lua/scheduler.lua:351: server/main.lua:234: attempt to index a nil value (field '?') stack traceback: server/main.lua:234: in upvalue 'ref' citizen:/scripting/lua/scheduler.lua:337: in function citizen:/scripting/lua/scheduler.lua:336 [C]: in function 'xpcall' citizen:/scripting/lua/scheduler.lua:336: in function citizen:/scripcfx ting/lua/scheduler.lua:335> stack traceback: [C]: in function 'error' citizen:/scripting/lua/scheduler.lua:351: in function citizen:/scripting/lua/scheduler.lua:322

function addGroupCommand(command, group, callback, callbackfailed, suggestion)
commands[command] = {}
commands[command].perm = math.maxinteger
commands[command].group = group
commands[command].cmd = callback
commands[command].callbackfailed = callbackfailed

if suggestion then
    if not suggestion.params or not type(suggestion.params) == "table" then suggestion.params = {} end
    if not suggestion.help or not type(suggestion.help) == "string" then suggestion.help = "" end

    commandSuggestions[command] = suggestion
end

ExecuteCommand('add_ace group.' .. group .. ' command.' .. command .. ' allow')

RegisterCommand(command, function(source, args)
    if groups[Users[source].getGroup()]:canTarget(group) then
        callback(source, args, Users[source])
    else
        callbackfailed(source, args, Users[source])
    end
end)

debugMsg("Group command added: " .. command .. ", requires group: " .. group)          end
点赞
用户10196418
用户10196418

Lua 只接受从 1 开始的索引,而当索引是 0 时通常会出现错误。因此,您可以检查 sourceUsers[source].getGroup() 是否可能等于 0,以确保它们始终 >= 1

2018-08-20 14:52:18
用户7134435
用户7134435

有点晚了,但为了后来者的参考:

当日志中出现像这样的错误时:

citizen:/scripting/lua/scheduler.lua:351: server/main.lua:234: attempt to index a nil value (field '?')`

往往是由于从数据库中获取的数据为空。

例如,

Error running call reference function for resource esx_identity: citizen:/scripting/lua/scheduler.lua:405: @esx_identity/server/main.lua:11: attempt to index a nil value (field '?')
stack traceback:
@esx_identity/server/main.lua:11: in upvalue 'ref'

esx_identity/server/main.lua中的第11行是:

if result[1].firstname ~= nil then

“firstname”引用的是从数据库返回的空值。SQL查询通常非常接近代码的问题行。

检查数据库中是否有包含空值的行,并删除或修复任何存在的行。

2019-06-30 15:18:24