1

DISCLAIMER: The title is bad but I dont know how else to word it

Issue: I am trying to compare entities in a FiveM script I am working on and tell if a player is close to a valid animal model and I cant get it to work and the only model being detected is the player its self

Current code:


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

Other things I have tried:

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
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)

Edit: I have tried a few other things aswell but i dont have those versions saved

Edit2: The current code the if statement seems like it doesnt even check anything its like all the code inside of it will run even if and the hashes dont match

Edit3: This also could be a distance calculation issue with the vector3 im not sure tho

Kyle Styx
  • 65
  • 5
  • try replacing `if animalHash == boar or deer or coyote then` with `if animalHash == boar or animalHash == deer or animalHash == coyote then` – freeve4h Dec 09 '22 at 18:49
  • and if that fails, please post a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)) – January Dec 12 '22 at 14:51

0 Answers0