lua - 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)
解决方案
@1 表索引为零,
好吧,第一个错误告诉您您正在使用 nil 值作为表索引。我不能告诉你是哪一个,因为你没有用行号发布整个错误消息。
它是由类似a = b[c]
或a = {[c] = b}
where c
is引起的nil
。
所以要么result[i].name
or result[i].item
isnil
当用于索引时shopItems
@2 尝试索引一个 nil 值(本地,xPlayer)
这更明显。xPlayer
因此nil
,您可能不会像在xPlayer.identifier
所以要么确保它xPlayer
永远不会nil
被索引,要么只在它是可索引的情况下索引它。
快速浏览 esx 手册显示
RegisterNetEvent('esx:playerLoaded')
AddEventHandler('esx:playerLoaded', function(playerData)
end)
所以根据手册只有一个论点。nil
当使用参数调用函数时,您有两个用作第二个。
推荐阅读
- reactjs - 只画了最后一个图的圆圈
- java - JPA 存储库在 @RestController 之外返回 null
- opengl - glslLangValidator 多个预处理器替换
- vue.js - 为 Vue.js 应用程序实现 D3 交互式和动态呈现的折线图
- azure-api-management - Azure API 管理获取用户身份?
- reactjs - ReactJS:在提交和更改时访问一个非常简单的表单输入值
- elasticsearch - 在 NEST 中使用 InMemoryConnection 并多次调用 Elasticsearch
- python - 如何将python变量的值存储在phpmyadmin的数据库中?
- mysql - 没有 index_list 的 USE INDEX() 有什么作用?
- r - 在R中按列创建级别统计?