首页 > 解决方案 > FiveM 服务器,表索引为 nil && 尝试索引 nil 值(本地,xPlayer)

问题描述

所以我为我的服务器获取了一些插件,并不断收到这两个错误。

这是我的两个代码。

1:表索引为零,

ESX.RegisterServerCallback('esx_weashop:requestDBItems', function(source, cb)

  MySQL.Async.fetchAll(
    'SELECT * FROM weashops',
    {},
    function(result)

      local shopItems  = {}

      for i=1, #result, 1 do

        if shopItems[result[i].name] == nil then
          shopItems[result[i].name] = {}
        end

        table.insert(shopItems[result[i].name], {
          name  = result[i].item,
          price = result[i].price,
          label = ESX.GetWeaponLabel(result[i].item)
        })
      end
      
      if Config.EnableClipGunShop == true then
        table.insert(shopItems["GunShop"], {
          name  = "clip",
          price = GunShopPrice,--Config.EnableClip.GunShop.Price,
          label = GunShopLabel--Config.EnableClip.GunShop.label
        })
        end
        
        if Config.EnableClipGunShop == true then
        table.insert(shopItems["BlackWeashop"], {
          name  = "clip",
          price = BlackWeashopPrice,--Config.EnableClip.BlackWeashop.Price,
          label = BlackWeashopLabel--Config.EnableClip.BlackWeashop.label
        })
        end
      cb(shopItems)

    end
  )

end)

2:尝试索引一个零值(本地,xPlayer)

AddEventHandler('esx:playerLoaded', function(playerId, xPlayer)
    MySQL.Async.fetchAll('SELECT status FROM users WHERE identifier = @identifier', {
        ['@identifier'] = xPlayer.identifier
    }, function(result)
        local data = {}

        if result[1].status then
            data = json.decode(result[1].status)
        end

        xPlayer.set('status', data)
        TriggerClientEvent('esx_status:load', playerId, data)
    end)
end)

标签: luafivem

解决方案


@1 表索引为零,

好吧,第一个错误告诉您您正在使用 nil 值作为表索引。我不能告诉你是哪一个,因为你没有用行号发布整个错误消息。

它是由类似a = b[c]a = {[c] = b}where cis引起的nil

所以要么result[i].nameor result[i].itemisnil当用于索引时shopItems

@2 尝试索引一个 nil 值(本地,xPlayer)

这更明显。xPlayer因此nil,您可能不会像在xPlayer.identifier

所以要么确保它xPlayer永远不会nil被索引,要么只在它是可索引的情况下索引它。

快速浏览 esx 手册显示

RegisterNetEvent('esx:playerLoaded')
AddEventHandler('esx:playerLoaded', function(playerData)

end)

所以根据手册只有一个论点。nil当使用参数调用函数时,您有两个用作第二个。


推荐阅读