Why are the enemy NPC jets not attacking?

I am fairly new to all of this so my script might be far from perfect, Anyways I am trying to make a script with enemy jets that would attack the player but they only seem to just fly around, They do not shoot guns or missile lock the player? Am I missing something?

local npcJets = {} -- Table to store jets
local attackRange = 1000.0 -- Distance within which the jets will attack
local jetModel = "hydra" -- Jet model to be used
local attackInterval = 5000 -- How often the jets attack (in milliseconds)
local jetsEnabled = false -- Toggle for enabling/disabling jet attack

-- Function to spawn an NPC jet with a pilot
function spawnNPCJet()
    local playerPed = PlayerPedId()
    local playerCoords = GetEntityCoords(playerPed)
    local model = GetHashKey(jetModel)

    -- Check if the jet model is loaded
    RequestModel(model)
    while not HasModelLoaded(model) do
        Citizen.Wait(500)
    end

    -- Spawn the jet at a random location near the player
    local jet = CreateVehicle(model, playerCoords.x + math.random(-200, 200), playerCoords.y + math.random(-200, 200), playerCoords.z, math.random(0, 360), true, false)

    -- Check if the vehicle was created successfully
    if DoesEntityExist(jet) then
        SetVehicleDoorsLocked(jet, 2) -- Lock the jet doors
        SetEntityAsMissionEntity(jet, true, true) -- Mark the vehicle as mission entity
        
        local pilotModel = GetHashKey("mp_m_freemode_01")
        
        -- Request the pilot model
        RequestModel(pilotModel)
        while not HasModelLoaded(pilotModel) do
            Citizen.Wait(500)
        end

        -- Create the NPC pilot inside the jet (as the driver)
        local pilotPed = CreatePedInsideVehicle(jet, 4, pilotModel, -1, true, true)

        -- Check if the pilot is created successfully
        if DoesEntityExist(pilotPed) then
            -- Ensure the NPC pilot is set to combat the player
            TaskCombatPed(pilotPed, playerPed, 0, 16)
            -- Task to fly towards the player
            TaskVehicleDriveToCoord(pilotPed, jet, playerCoords.x, playerCoords.y, playerCoords.z, 150.0, 0, GetEntityModel(jet), 262144, 1.0, true)
            print("Jet and pilot spawned successfully!") -- Debug message
        else
            print("Failed to create pilot.") -- Debug message
        end

        table.insert(npcJets, jet)
    else
        print("Failed to spawn jet.") -- Debug message
    end
end

-- Function to make the jets attack the player
function attackPlayerWithJets()
    local playerPed = PlayerPedId()
    local playerCoords = GetEntityCoords(playerPed)

    for _, jet in ipairs(npcJets) do
        if DoesEntityExist(jet) then
            local jetCoords = GetEntityCoords(jet)
            local distance = Vdist(playerCoords.x, playerCoords.y, playerCoords.z, jetCoords.x, jetCoords.y, jetCoords.z)

            if distance < attackRange then
                local pilotPed = GetPedInVehicleSeat(jet, -1) -- Get the pilot of the jet
                if DoesEntityExist(pilotPed) then
                    -- Make the NPC pilot attack the player
                    TaskCombatPed(pilotPed, playerPed, 0, 16) -- Engage combat with the player

                    -- Make the jet follow the player and stay within attack range
                    TaskVehicleDriveToCoord(pilotPed, jet, playerCoords.x, playerCoords.y, playerCoords.z, 150.0, 0, GetEntityModel(jet), 262144, 1.0, true)
                end
            end
        end
    end
end

-- Function to despawn all jets and their pilots
function despawnJets()
    for _, jet in ipairs(npcJets) do
        if DoesEntityExist(jet) then
            local pilotPed = GetPedInVehicleSeat(jet, -1) -- Get the pilot of the jet
            if DoesEntityExist(pilotPed) then
                DeleteEntity(pilotPed) -- Delete the NPC pilot
                print("Pilot despawned.") -- Debug message
            end
            DeleteEntity(jet) -- Delete the jet
            print("Jet despawned.") -- Debug message
        end
    end
    npcJets = {} -- Clear the table
end

-- Function to enable/disable the jet attack
RegisterCommand('jet', function()
    if jetsEnabled then
        jetsEnabled = false
        TriggerEvent('chat:addMessage', {
            args = { "^1Jet attacks disabled." }
        })
        despawnJets() -- Call the despawn function when disabling
    else
        jetsEnabled = true
        TriggerEvent('chat:addMessage', {
            args = { "^2Jet attacks enabled." }
        })
    end
end, false)

-- Timer to spawn jets (only when enabled)
Citizen.CreateThread(function()
    while true do
        Citizen.Wait(attackInterval) -- Wait before spawning next jet
        if jetsEnabled then
            spawnNPCJet()
        end
    end
end)

-- Timer to make jets attack (only when enabled)
Citizen.CreateThread(function()
    while true do
        Citizen.Wait(attackInterval) -- Check every few seconds
        if jetsEnabled then
            attackPlayerWithJets()
        end
    end
end)

Have you tried TASK_PLANE_MISSION with the CTaskVehicleAttack parameter?

1 Like

From my understanding of the Task system, those natives typically override current Task actions, so you are calling TaskCombatPed, then immediately overriding that action with another Task.
Niclqs provided what I think is going to be the best native available to you, given the ability to adjust parameters of the Task.

1 Like

It appears to be AI generated, careful as AI is far from perfect with lua code.
Using TaskCombatPed isn’t used for making a plane attack.
You would need to use the TaskPlaneMission native.