[英]attempt to compare a number with nil
我遇到以下錯誤的問題:
esx_glovebox_sv.lua:138:嘗試將數字與 nil 進行比較。
第 138 行是下面 RAW 數據中的第三行
RegisterServerEvent("esx_glovebox:getItem")
AddEventHandler(
"esx_glovebox:getItem",
function(plate, type, item, count, max, owned)
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
if type == "item_standard" then
local targetItem = xPlayer.getInventoryItem(item)
if targetItem.limit == -1 or ((targetItem.count + count) <= targetItem.limit) then
TriggerEvent(
"esx_glovebox:getSharedDataStore",
plate,
function(store)
local coffres = (store.get("coffres") or {})
for i = 1, #coffres, 1 do
if coffres[i].name == item then
if (coffres[i].count >= count and count > 0) then
xPlayer.addInventoryItem(item, count)
if (coffres[i].count - count) == 0 then
table.remove(coffres, i)
else
coffres[i].count = coffres[i].count - count
end
break
else
TriggerClientEvent(
"pNotify:SendNotification",
_source,
{
text = _U("invalid_quantity"),
type = "error",
queue = "glovebox",
timeout = 3000,
layout = "bottomCenter"
}
)
end
如果我正確理解您的帖子,“第 138 行”指向您發布的代碼片段中的第三行,即:
if targetItem.limit == -1 or ((targetItem.count + count) <= targetItem.limit) then
錯誤意味着,您正在使用的值之一nil
,因此無法與數字進行比較。 在您的情況下,這只能是targetItem.limit
。
如果每個targetItem
都應該有一個limit
和count
數值,那么問題就出在代碼的其他地方。
您可以通過添加額外的檢查來簡單地檢查值的存在,而不是拋出錯誤:
if type == "item_standard" then
local targetItem = xPlayer.getInventoryItem(item)
-- Make sure that targetItem and targetItem.limit aren't nil.
if targetItem and targetItem.limit then
if targetItem.limit == -1 or ((targetItem.count + count) <= targetItem.limit) then
簡短說明:在 Lua 中nil
和布爾值false
表示邏輯表達式中的false
值。 任何其他值都將被視為true
。 在這種情況下,如果targetItem
或targetItem.limit
為nil
您將跳過嵌套的 if 語句。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.