Does anyone know how to prevent the animals from being spawned on the roofs?

Citizen.CreateThread(function()
    while true do
        Wait(0)

        local player = PlayerPedId()
        local pos = GetEntityCoords(player, true)
        local ground

        for animal, properties in pairs(Config.Animals) do
            if Config.Animals[animal][GetNameOfZone(pos)] ~= nil then
                if #entities < 25 then
                    RequestModel(animal)
                    while not HasModelLoaded(animal) or not HasCollisionForModelLoaded(animal) do
                        Wait(1)
                    end

                    local posX = pos.x + math.random(-100, 100)
                    local posY = pos.y + math.random(-100, 100)
                    local Z = pos.z + 999.0
                    local heading = math.random(0, 359) + .0

                    ground, posZ = GetGroundZFor_3dCoord(posX + .0, posY + .0, Z, 1)

                    if ground then
                        local ped = CreatePed(28, animal, posX, posY, posZ, heading, true, true)
                        print(GetEntityCoords(ped))
                        print(posZ)
                        SetEntityAsMissionEntity(ped, true, true)

                        TaskWanderStandard(ped, 10.0, 10)
                        
                        table.insert(entities, {entity = ped, name = animal})
                    end
                end
            end
        end
    end
end)

Does anyone know how to prevent the animals from being spawned on the roofs? It’s quite annoying and I haven’t found anything to prevent it

Try incorporating GetSafeCoordForPed into the entity spawn logic, might help

If I implement this, it is always false. Did I implement it correctly (new coords are always (0.0, 0.0, 0.0))?

Citizen.CreateThread(function()
    while true do
        Wait(0)

        local player = PlayerPedId()
        local pos = GetEntityCoords(player, true)
        local ground

        for animal, properties in pairs(Config.Animals) do
            if Config.Animals[animal][GetNameOfZone(pos)] ~= nil then
                if #entities < 25 then
                    RequestModel(animal)
                    while not HasModelLoaded(animal) or not HasCollisionForModelLoaded(animal) do
                        Wait(1)
                    end

                    local posX = pos.x + math.random(-100, 100)
                    local posY = pos.y + math.random(-100, 100)

                    ground, posZ = GetGroundZFor_3dCoord(posX + .0, posY + .0, pos.z + 999.0, 1)

                    local safeCoords, newCoords = GetSafeCoordForPed(posX, posY, posZ, true, 16)
                    print(safeCoords)
                    if safeCoords then
                        print("Spawn")
                        posX, posY, posZ = table.unpack(newCoords)
                        local ped = CreatePed(28, animal, posX, posY, posZ, heading, true, true)
                        print(GetEntityCoords(ped))
                        print(GetEntityCoords(ped))
                        SetEntityAsMissionEntity(ped, true, true)

                        TaskWanderStandard(ped, 10.0, 10)
                        
                        table.insert(entities, {entity = ped, name = animal})
                    end
                end
            end
        end

        for i, ped in ipairs(entities) do
            Wait(1)
            local dist = GetDistanceBetweenCoords(GetEntityCoords(player), GetEntityCoords(ped.entity), true)
            if dist < 20.0 then
                ClearPedTasks(ped.entity)

                Wait(100)

                TaskCombatPed(ped.entity, PlayerPedId(), 0, 16)
                SetRelationshipBetweenGroups(5, GetHashKey(ped.name), PlayerPedId())
                SetRelationshipBetweenGroups(5, PlayerPedId(), GetHashKey(ped.name))
                SetPedAsNoLongerNeeded(ped.entity)
                table.remove(entities, i)
            end

            if dist > 120.0 then
                SetPedAsNoLongerNeeded(ped.entity)
            end

            if IsEntityInWater(ped.entity) then
                local model = GetEntityModel(ped.entity)
                SetEntityAsNoLongerNeeded(ped.entity)
                SetModelAsNoLongerNeeded(model)
                DeleteEntity(ped.entity)
                table.remove(entities, i)
            end
        end
    end
end)

Thank you very much I got it solved. The problem was the boolean in GetSafeCoordForPed

Citizen.CreateThread(function()
    while true do
        Wait(0)

        local player = PlayerPedId()
        local pos = GetEntityCoords(player, true)
        local ground

        for animal, properties in pairs(Config.Animals) do
            if Config.Animals[animal][GetNameOfZone(pos)] ~= nil then
                if #entities < 25 then
                    RequestModel(animal)
                    while not HasModelLoaded(animal) or not HasCollisionForModelLoaded(animal) do
                        Wait(1)
                    end

                    local posX = pos.x + math.random(-100, 100)
                    local posY = pos.y + math.random(-100, 100)

                    ground, posZ = GetGroundZFor_3dCoord(posX + .0, posY + .0, pos.z + 999.0, 1)

                    local safeCoords, newCoords = GetSafeCoordForPed(posX, posY, posZ, false, 16)
                    if safeCoords then
                        posX, posY, posZ = table.unpack(newCoords)
                        local ped = CreatePed(28, animal, posX, posY, posZ, heading, true, true)
                        SetEntityAsMissionEntity(ped, true, true)

                        TaskWanderStandard(ped, 10.0, 10)
                        
                        table.insert(entities, {entity = ped, name = animal})
                    end
                end
            end
        end

        for i, ped in ipairs(entities) do
            Wait(1)
            local dist = GetDistanceBetweenCoords(GetEntityCoords(player), GetEntityCoords(ped.entity), true)
            if dist < 20.0 then
                ClearPedTasks(ped.entity)

                Wait(100)

                TaskCombatPed(ped.entity, PlayerPedId(), 0, 16)
                SetRelationshipBetweenGroups(5, GetHashKey(ped.name), PlayerPedId())
                SetRelationshipBetweenGroups(5, PlayerPedId(), GetHashKey(ped.name))
                SetPedAsNoLongerNeeded(ped.entity)
                table.remove(entities, i)
            end

            if dist > 120.0 then
                SetPedAsNoLongerNeeded(ped.entity)
            end

            if IsEntityInWater(ped.entity) then
                local model = GetEntityModel(ped.entity)
                SetEntityAsNoLongerNeeded(ped.entity)
                SetModelAsNoLongerNeeded(model)
                DeleteEntity(ped.entity)
                table.remove(entities, i)
            end
        end
    end
end)
1 Like