尝试索引全局变量“GameDatabase”(一个空值)-(一个Garrysmod脚本)

我有些困惑为什么这个不起作用。我没有对代码做任何更改,所以我有点好奇为什么它不再工作。任何帮助将不胜感激。以下是我得到的错误:

    [ERROR] gamemodes/harborrp/gamemode/data.lua:59: attempt to index global 'GameDatabase' (a nil value)
  1. Query - gamemodes/harborrp/gamemode/data.lua:59
   2. unknown - gamemodes/harborrp/gamemode/sv_gamemode_functions.lua:790
    3. UniqueID - [C]:-1
     4. echoToAdmins - addons/ulx/lua/ulx/log.lua:164
      5. fn - addons/ulx/lua/ulx/log.lua:202
       6. unknown - addons/ulib/lua/ulib/shared/hook.lua:110

[ERROR] gamemodes/harborrp/gamemode/data.lua:158: attempt to index global 'GameDatabase' (a nil value)
  1. QueryValue - gamemodes/harborrp/gamemode/data.lua:158
   2. LoadInventory - gamemodes/harborrp/gamemode/inventory/sv_inventory.lua:41
    3. fn - gamemodes/harborrp/gamemode/inventory/sv_inventory.lua:58
     4. unknown - addons/ulib/lua/ulib/shared/hook.lua:110

[ERROR] gamemodes/harborrp/gamemode/data.lua:158: attempt to index global 'GameDatabase' (a nil value)
  1. QueryValue - gamemodes/harborrp/gamemode/data.lua:158
   2. unknown - gamemodes/harborrp/gamemode/sv_gamemode_functions.lua:1068

这里是它所指的地方:

第58行:

function DB.Query(query, callback, qtype)

local cBackFunc = function(r)
    if r[1].error then
        local err = table.concat({"tMySQL query error: ", r[1].error, debug.Trace() or "", "Query: ", query, "\n"}, "")
        DB.Log(err)
        ErrorNoHalt(err)
        r[1].data = nil
        //return
    end
    // lua_run DB.Query("SELECT amount, steam, rpname FROM metrorp_wallets limit 3", PrintTable, 1)
    if callback then
        if qtype == 0 then
            ErrorNoHalt("Query "..query.." used Type 0 in DB.Query!\n")
            local tbl = {}
            for i=1, #r[1].data do
                local ind = 1
                tbl[i] = {}
                for a,b in pairs(r[1].data[i]) do
                    tbl[i][ind] = b
                    ind = ind + 1
                end
            end
            callback(tbl, !r[1].error)
        else
            callback(r[1].data, !r[1].error)
        end
    end

end
GameDatabase:Query(query, cBackFunc)

end

第158行:

function DB.QueryValue(query, callback)
local function cBack(r)
    if !r or !r[1].data or !r[1].data[1] then
        callback(nil, !r[1].error)
        return
    end
    local ind = next(r[1].data[1])
    callback(r[1].data[1][ind], !r[1].error)
end
GameDatabase:Query(query, callback and cBack)

end

点赞
用户3492195
用户3492195

GameDatabasenil。可能是因为你尝试在客户端运行此代码,而 GameDatabase 只是在服务器端声明的。你可以在共享代码周围放置一个 if SERVER then 检查,以便仅在服务器端运行它。

2016-04-17 22:55:04