FiveM Lua脚本问题:无法正确检测附近有效动物实体
FiveM动物检测脚本问题解决
问题概述
开发FiveM脚本时,尝试判断玩家是否靠近有效动物模型,但功能异常,仅能检测到玩家自身,且if语句逻辑失效,哈希不匹配时内部代码仍执行。
当前代码
local QBCore = exports['qb-core']:GetCoreObject() local animals = GetGamePool('CPed') local hasTool = false local hasKnife = QBCore.Functions.HasItem('Knife', function(result) if result then hasTool = true hasKnife = true end end) for k, v in pairs(animals) do local animal = animals[k] local animalModel = GetEntityModel(animal) local animalHash = GetHashKey(animal) local boar = GetHashKey('a_c_boar') local deer = GetHashKey('a_c_deer') local coyote = GetHashKey('a_c_coyote') print("Deer: " .. deer .. "boar: " .. boar .. "coyote: " .. coyote) if animalHash == boar or deer or coyote then local entPos = GetEntityCoords(v) local playerPos = GetEntityCoords(PlayerPedId()) if #(entPos - playerPos) < 1.0 then foundAnimal = v print("Animal close by" .. animalHash) break end else print("No animals found") end end
尝试过的其他方案
方案1
local QBCore = exports['qb-core']:GetCoreObject() local animals = GetGamePool('CPed') local validAnimals = { ["a_c_boar"] = true, ["a_c_coyote"] = true, ["a_c_crow"] = true, ["a_c_deer"] = true, ["a_c_mtlion"] = true, ["a_c_pig"] = true, ["a_c_rabbit_01"] = true, } local hasTool = false local hasKnife = QBCore.Functions.HasItem('Knife', function(result) if result then hasTool = true hasKnife = true end end) for k, v in pairs(animals) do local animal = animals[k] local animalModel = GetEntityModel(animal) local animalModelName = GetEntityModel(animalModel) if validAnimals[animalModelName] then local entPos = GetEntityCoords(v) local playerPos = GetEntityCoords(PlayerPedId()) if #(entPos - playerPos) < 2.0 then foundAnimal = v print("Found animal" .. foundAnimal .. "!" .. "Model: " .. entModel) break end end
方案2
local QBCore = exports['qb-core']:GetCoreObject() Citizen.CreateThread(function () local animals = GetGamePool('CPed') local validAnimals = { ["a_c_boar"] = true, ["a_c_coyote"] = true, ["a_c_crow"] = true, ["a_c_deer"] = true, ["a_c_mtlion"] = true, ["a_c_pig"] = true, ["a_c_rabbit_01"] = true, } local hasTool = false local hasKnife = QBCore.Functions.HasItem('Knife', function(result) if result then hasTool = true hasKnife = true end end) for k, v in pairs(animals) do local entModel = GetEntityModel(v) if validAnimals[entModel] then local entPos = GetEntityCoords(v) local playerPos = GetEntityCoords(PlayerPedId()) if #(entPos - playerPos) < 2.0 then foundAnimal = v print("Found animal" .. foundAnimal .. "!" .. " " .. "Model: " .. entModel) break end end)
补充说明
- 还尝试过其他方案,但未保存版本
- 当前代码的if语句似乎未生效,即使哈希不匹配,内部代码仍会执行
- 也可能是vector3的距离计算存在问题
问题根源与修正方案
核心问题点
- 条件判断逻辑错误:
if animalHash == boar or deer or coyote写法完全错误,deer和coyote作为非空值会被Lua判定为true,导致所有Ped都进入判断分支,包括玩家自身。 - 实体哈希获取错误:
GetHashKey(animal)是获取实体的哈希值,而非模型哈希,应该用GetEntityModel(v)直接获取模型哈希。 - 未排除玩家自身:
GetGamePool('CPed')会返回所有Ped实体,包括玩家自己,必须过滤掉。 - 模型匹配方式错误:用模型字符串作为键时,未正确将模型哈希转换为对应名称;直接用哈希匹配更可靠。
- 异步函数使用不当:
QBCore.Functions.HasItem是异步函数,同步代码中无法直接获取结果,需将检测逻辑放入回调内。 - 一次性执行逻辑无效:游戏内实体动态变化,一次性循环无法持续检测,需放入线程循环。
修正后的完整代码
local QBCore = exports['qb-core']:GetCoreObject() -- 预定义有效动物模型哈希(直接用哈希匹配,避免字符串转换错误) local validAnimalHashes = { GetHashKey('a_c_boar'), GetHashKey('a_c_coyote'), GetHashKey('a_c_crow'), GetHashKey('a_c_deer'), GetHashKey('a_c_mtlion'), GetHashKey('a_c_pig'), GetHashKey('a_c_rabbit_01') } -- 工具检测完成后再执行动物检测逻辑 QBCore.Functions.HasItem('Knife', function(hasKnife) if not hasKnife then return end -- 创建循环线程持续检测游戏内实体 CreateThread(function() while true do local foundAnimal = nil local playerPed = PlayerPedId() local playerPos = GetEntityCoords(playerPed) local allPeds = GetGamePool('CPed') for _, ped in pairs(allPeds) do -- 跳过玩家自身Ped if ped ~= playerPed then local pedModel = GetEntityModel(ped) -- 检查当前Ped是否为有效动物 for _, hash in pairs(validAnimalHashes) do if pedModel == hash then local pedPos = GetEntityCoords(ped) -- 距离计算逻辑没问题,直接用向量减法的长度即可 if #(pedPos - playerPos) < 2.0 then foundAnimal = ped print("发现有效动物!实体ID:" .. foundAnimal .. " 模型哈希:" .. pedModel) break end end end if foundAnimal then break end end end Wait(500) -- 每0.5秒检测一次,平衡性能与实时性 end end) end)
修正说明
- 改用模型哈希直接匹配,避免字符串转换带来的错误
- 修复条件判断逻辑,正确对比每个模型哈希
- 加入玩家自身过滤,彻底排除误判
- 将检测逻辑放入
HasItem的异步回调中,确保工具检测完成后再执行 - 使用线程循环持续检测,适配游戏内动态变化的实体
- 增加
Wait降低性能消耗,避免占用过多资源
内容的提问来源于stack exchange,提问作者Kyle Styx
相关产品推荐
相关产品推荐

