FiveM Lua售毒脚本报错求助:参数1为空
FiveM售毒脚本错误排查与修复
错误根源分析
- 核心问题:
SellDrugs函数中错误使用全局Player对象(大写P),但实际传入的是局部变量player(小写p),导致调用Player.Functions.GetItemByName时对象为空,触发了"Argument at index 1 was null"的错误。 - 未定义
policeChance变量,后续警察触发逻辑会报错。 QBCore.Functions.Notify参数顺序错误,success是未定义变量,需替换为字符串类型的提示类型。- 代码中存在HTML转义字符
",需替换为正常双引号"。
修复后的完整代码
RegisterServerEvent('aim_exchange:sellDrugs') AddEventHandler('aim_exchange:sellDrugs', function() local src = source local player = QBCore.Functions.GetPlayer(src) SellDrugs(player) end) -- 定义警察触发概率,可自行调整数值 local policeChance = 20 function SellDrugs(player) local drugs = { { name = "coke_small_brick", price = math.random(1, 50), amount = math.random(1, 3), }, } local drug = nil for _, drugData in ipairs(drugs) do local itemName = drugData.name local itemPrice = drugData.price -- 使用传入的player对象(小写p)调用物品函数 local foundItem = player.Functions.GetItemByName(itemName) if foundItem and foundItem.amount >= drugData.amount then drug = { name = itemName, price = itemPrice, amount = drugData.amount } break end end if drug then -- 统一使用player对象操作背包 player.Functions.RemoveItem(drug.name, drug.amount) player.Functions.AddItem("dirtymoney", drug.price) -- 修正Notify参数格式,指定正确的提示类型 QBCore.Functions.Notify('Drug sold successfully.', "success", 10000) else QBCore.Functions.Notify('You do not have enough drugs to sell.', "error", 10000) end -- 使用已定义的警察触发概率变量 if math.random(1, 100) <= policeChance then CallPolice() end end
额外说明
修复后脚本将正常遍历玩家背包、扣除对应毒品数量并发放脏钱,同时按设定概率触发警察预警逻辑。若CallPolice函数未实现,需自行补充对应的警察通缉触发代码。
内容的提问来源于stack exchange,提问作者Not Aimeri
相关产品推荐
相关产品推荐

