Dogfight system Planes dont Attack the player nor shoot

i was trying to make a simple dogfight System, with both the P-45 nokata (only machine guns no missiles) and P-996 lazer (lock on missiles) and i made the basic stuff work, but they never attacked the player in any way, how can i do so?

Clean version

– server:

RegisterCommand("airraid", function(source, args, raw)
    TriggerClientEvent("airraid:spawnPlane", -1)
end, true)

– client:

local spawnedPlanes = {}

RegisterNetEvent("airraid:spawnPlane")
AddEventHandler("airraid:spawnPlane", function()
    local playerPed = PlayerPedId()
    local playerCoords = GetEntityCoords(playerPed)

    local model = `lazer`
    local pilotModel = `s_m_m_pilot_02`

    RequestModel(model)
    RequestModel(pilotModel)
    while not HasModelLoaded(model) or not HasModelLoaded(pilotModel) do
        Wait(10)
    end

    local spawnRadius = 600.0 
    local angle = math.random() * math.pi * 2
    local spawnX = playerCoords.x + math.cos(angle) * spawnRadius
    local spawnY = playerCoords.y + math.sin(angle) * spawnRadius
    local spawnZ = playerCoords.z + 150.0 

    local plane = CreateVehicle(model, spawnX, spawnY, spawnZ, math.random(0, 360), true, true)
    SetVehicleEngineOn(plane, true, true, false)
    SetEntityInvincible(plane, false)
    SetVehicleLivery(plane, math.random(0, 2))
    SetVehicleForwardSpeed(plane, 120.0)
    SetEntityAsMissionEntity(plane, true, false)
    SetPlaneTurbulenceMultiplier(plane, 0.5)
    SetVehicleCanBreak(plane, true)
    SetVehicleDirtLevel(plane, 5.0)
    SetVehicleLandingGear(plane, 3)

    local pilot = CreatePedInsideVehicle(plane, 1, pilotModel, -1, true, false)
    SetPedKeepTask(pilot, true)
    SetPedAsEnemy(pilot, false)
    SetEntityInvincible(pilot, false)
    SetBlockingOfNonTemporaryEvents(pilot, true)


    local destRadius = 3000.0
    local destAngle = math.random() * math.pi * 2
    local destX = spawnX + math.cos(destAngle) * destRadius
    local destY = spawnY + math.sin(destAngle) * destRadius
    local destZ = spawnZ + math.random(-40, 40)

    TaskPlaneMission(
        pilot, plane, 0, 0,
        destX, destY, destZ,
        4,  -- mission type: fly to
        400.0,  -- cruise speed
        0,  -- targetVehicle
        GetEntityHeading(plane),
        10.0, 10.0, 50.0
    )

   
    local blip = AddBlipForEntity(plane)
    SetBlipSprite(blip, 16)
    SetBlipColour(blip, 1)
    SetBlipScale(blip, 1.0)
    BeginTextCommandSetBlipName("STRING")
    AddTextComponentString("Aereo Ostile")
    EndTextCommandSetBlipName(blip)

    table.insert(spawnedPlanes, {plane = plane, pilot = pilot, blip = blip})

 
    CreateThread(function()
        while DoesEntityExist(plane) do
            Wait(2000)
            -- Se l'aereo o il pilota muoiono, rimuoviamo solo il blip, lasciando i detriti
            if IsEntityDead(pilot) or IsEntityDead(plane) then
                RemoveBlip(blip)

                break
            end
        end
    end)
end)

Your problem is this part:

TaskPlaneMission(pilot, plane, 0, 0, destX, destY, destZ, 4, 400.0, 0, GetEntityHeading(plane), 10.0, 10.0, 50.0)

That’s only a flight order – not a combat command. The pilot doesn’t know he should attack the player. GTA NPCs only fight if they have a target and the proper AI task, like TaskCombatPed, TaskPlaneChase or TaskVehicleShootAtPed.

Add this logic after creating the pilot:

local playerPed = PlayerPedId()
SetPedRelationshipGroupHash(pilot, GetHashKey(“HATES_PLAYER”))
SetPedCanBeTargetted(pilot, true)
SetCanAttackFriendly(pilot, false, true)
GiveWeaponToPed(pilot, weapon_vehicle_rocket, 9999, false, true)
SetCurrentPedWeapon(pilot, weapon_vehicle_rocket, true)

– AI attack order
TaskPlaneChase(pilot, playerPed)

– or more aggressive
– TaskCombatPed(pilot, playerPed, 0, 16)

TaskPlaneChase makes the plane actually chase and attack the player.
If you only want MGs (like the P-45 Nokota), use TaskVehicleShootAtPed instead.

GreetZ
NebelRebell
Nebelbank.net

like this?

local spawnedPlanes = {}

RegisterNetEvent("airraid:spawnPlane")
AddEventHandler("airraid:spawnPlane", function()
    local playerPed = PlayerPedId()
    local playerCoords = GetEntityCoords(playerPed)

    local planeModel = `lazer`
    local pilotModel = `s_m_m_pilot_02`

    RequestModel(planeModel)
    RequestModel(pilotModel)
    while not HasModelLoaded(planeModel) or not HasModelLoaded(pilotModel) do
        Wait(10)
    end

    -- coordinate di spawn
    local spawnRadius = 600.0
    local angle = math.random() * math.pi * 2
    local spawnX = playerCoords.x + math.cos(angle) * spawnRadius
    local spawnY = playerCoords.y + math.sin(angle) * spawnRadius
    local spawnZ = playerCoords.z + 150.0

    -- crea l'aereo
    local plane = CreateVehicle(planeModel, spawnX, spawnY, spawnZ, math.random(0, 360), true, true)
    SetEntityAsMissionEntity(plane, true, false)
    SetVehicleEngineOn(plane, true, true, false)
    SetPlaneTurbulenceMultiplier(plane, 0.5)
    SetVehicleCanBreak(plane, true)
    SetVehicleDirtLevel(plane, 5.0)
    SetVehicleLandingGear(plane, 3)
    SetVehicleForwardSpeed(plane, 120.0)

    -- crea il pilota
    local pilot = CreatePedInsideVehicle(plane, 1, pilotModel, -1, true, false)
    SetPedKeepTask(pilot, true)
    SetPedAsEnemy(pilot, true)
    SetEntityInvincible(pilot, false)
    SetBlockingOfNonTemporaryEvents(pilot, false)
    SetPedRelationshipGroupHash(pilot, GetHashKey("HATES_PLAYER"))
    SetPedCanBeTargetted(pilot, true)
    SetCanAttackFriendly(pilot, false, true)

    -- arma al pilota (missili)
    GiveWeaponToPed(pilot, `WEAPON_VEHICLE_ROCKET`, 9999, false, true)
    SetCurrentPedWeapon(pilot, `WEAPON_VEHICLE_ROCKET`, true)

    -- attacco al giocatore
    local target = playerPed
    TaskPlaneChase(pilot, target)
    -- alternativa aggressiva: TaskCombatPed(pilot, target, 0, 16)

    -- blip visibile globalmente
    local blip = AddBlipForEntity(plane)
    SetBlipSprite(blip, 16)
    SetBlipColour(blip, 1)
    SetBlipScale(blip, 1.0)
    BeginTextCommandSetBlipName("STRING")
    AddTextComponentString("Aereo Ostile")
    EndTextCommandSetBlipName(blip)

    table.insert(spawnedPlanes, {plane = plane, pilot = pilot, blip = blip})

    -- thread per monitorare distruzione
    CreateThread(function()
        while DoesEntityExist(plane) do
            Wait(2000)
            if IsEntityDead(pilot) or IsEntityDead(plane) then
                if DoesBlipExist(blip) then
                    RemoveBlip(blip)
                end
                break
            end
        end
    end)
end)

also i kinda need it multiplayer and synced

edit:tried, it kinda does the same thing as before. random movement.

edit2:looked it better, they sometimes attack, they don’t 90% (50 with the aggressive version)

Edit3: tbh im fine with It thx so much. I Just didnt understand how to make It work on the nokota

Ty Bro, i got everything but i didnt understand how the nokota mgs

Hey, nice — glad you got most of it working! The Nokota (P-45) only uses built-in machine guns, not rockets like the Lazer, so giving the pilot a rocket weapon won’t work. You just need to arm it with WEAPON_VEHICLE_MACHINEGUN and force the AI to engage the player.

Here’s the full working version for the Nokota, ready to drop in:

RegisterNetEvent("airraid:spawnNokota")
AddEventHandler("airraid:spawnNokota", function()
    local playerPed = PlayerPedId()
    local playerCoords = GetEntityCoords(playerPed)

    local planeModel = `nokota`
    local pilotModel = `s_m_m_pilot_02`

    RequestModel(planeModel)
    RequestModel(pilotModel)
    while not HasModelLoaded(planeModel) or not HasModelLoaded(pilotModel) do
        Wait(10)
    end

    local spawnRadius = 600.0
    local angle = math.random() * math.pi * 2
    local spawnX = playerCoords.x + math.cos(angle) * spawnRadius
    local spawnY = playerCoords.y + math.sin(angle) * spawnRadius
    local spawnZ = playerCoords.z + 150.0

    local plane = CreateVehicle(planeModel, spawnX, spawnY, spawnZ, math.random(0, 360), true, true)
    SetEntityAsMissionEntity(plane, true, false)
    SetVehicleEngineOn(plane, true, true, false)
    SetVehicleForwardSpeed(plane, 120.0)
    SetVehicleLandingGear(plane, 3)
    SetPlaneTurbulenceMultiplier(plane, 0.3)
    SetVehicleDirtLevel(plane, 3.0)

    local pilot = CreatePedInsideVehicle(plane, 1, pilotModel, -1, true, false)
    SetPedKeepTask(pilot, true)
    SetPedAsEnemy(pilot, true)
    SetPedRelationshipGroupHash(pilot, GetHashKey("HATES_PLAYER"))
    SetCanAttackFriendly(pilot, false, true)
    SetBlockingOfNonTemporaryEvents(pilot, false)
    SetPedAccuracy(pilot, 75)
    SetEntityInvincible(pilot, false)

    -- Give MGs to pilot
    GiveWeaponToPed(pilot, `WEAPON_VEHICLE_MACHINEGUN`, 9999, false, true)
    SetCurrentPedWeapon(pilot, `WEAPON_VEHICLE_MACHINEGUN`, true)

    -- Make AI chase and attack player
    TaskPlaneChase(pilot, playerPed)

    -- Blip setup
    local blip = AddBlipForEntity(plane)
    SetBlipSprite(blip, 16)
    SetBlipColour(blip, 1)
    SetBlipScale(blip, 1.0)
    BeginTextCommandSetBlipName("STRING")
    AddTextComponentString("Hostile Nokota")
    EndTextCommandSetBlipName(blip)

    -- Monitor destruction
    CreateThread(function()
        while DoesEntityExist(plane) do
            Wait(2000)
            if IsEntityDead(pilot) or IsEntityDead(plane) then
                if DoesBlipExist(blip) then RemoveBlip(blip) end
                break
            end
        end
    end)
end)
Trigger it from the server side with:
TriggerClientEvent("airraid:spawnNokota", -1)

This version makes the Nokota actually use its machine guns against players in multiplayer, synced and aggressive like a real dogfight.

Hope it works!

GreetZ,
NebelRebell
Nebelbank.net

Thx bro but why does It sound kinda like ai??? :joy:

Yeah, I know — I used a translator, just wanted to make sure I’m understood. :joy:

Sometimes it’s boring, for example:
“Da steppt der Bär!”
→ Literally: The bear is dancing there!
→ Figuratively: That place is really lively!
→ Result: Sounds like a documentary about circus animals on LSD.

or

“Da liegt der Hund begraben.”
→ Literally: That’s where the dog is buried.
→ Figuratively: That’s the heart of the matter.
→ Result: In English, this sounds more like the introduction to a crime novel.

've a nice day, Dude :wink:

Feels right abt right, anyway the code u gave me feels weird

yo bro i need smth, i know its a long time but today, without changing anything from the old script, just stopped working. wth happend?

local spawnedPlanes = {}

RegisterNetEvent(“airraid:spawnPlane”)
AddEventHandler(“airraid:spawnPlane”, function()
local playerPed = PlayerPedId()
local playerCoords = GetEntityCoords(playerPed)

local planeModel = lazer
local pilotModel = s_m_m_pilot_02

RequestModel(planeModel)
RequestModel(pilotModel)
while not HasModelLoaded(planeModel) or not HasModelLoaded(pilotModel) do
    Wait(10)
end

-- coordinate di spawn
local spawnRadius = 600.0
local angle = math.random() * math.pi * 2
local spawnX = playerCoords.x + math.cos(angle) * spawnRadius
local spawnY = playerCoords.y + math.sin(angle) * spawnRadius
local spawnZ = playerCoords.z + 150.0

-- crea l'aereo
local plane = CreateVehicle(planeModel, spawnX, spawnY, spawnZ, math.random(0, 360), true, true)
SetEntityAsMissionEntity(plane, true, false)
SetVehicleEngineOn(plane, true, true, false)
SetPlaneTurbulenceMultiplier(plane, 0.5)
SetVehicleCanBreak(plane, true)
SetVehicleDirtLevel(plane, 5.0)
SetVehicleLandingGear(plane, 3)
SetVehicleForwardSpeed(plane, 120.0)

-- crea il pilota
local pilot = CreatePedInsideVehicle(plane, 1, pilotModel, -1, true, false)
SetPedKeepTask(pilot, true)
SetPedAsEnemy(pilot, true)
SetEntityInvincible(pilot, false)
SetBlockingOfNonTemporaryEvents(pilot, false)
SetPedRelationshipGroupHash(pilot, GetHashKey("HATES_PLAYER"))
SetPedCanBeTargetted(pilot, true)
SetCanAttackFriendly(pilot, false, true)

-- arma al pilota (missili)
GiveWeaponToPed(pilot, WEAPON_VEHICLE_ROCKET, 9999, false, true)
SetCurrentPedWeapon(pilot, WEAPON_VEHICLE_ROCKET, true)

-- attacco al giocatore
local target = playerPed
TaskCombatPed(pilot, target, 0, 16)

-- blip visibile globalmente
local blip = AddBlipForEntity(plane)
SetBlipSprite(blip, 16)
SetBlipColour(blip, 1)
SetBlipScale(blip, 1.0)
BeginTextCommandSetBlipName("STRING")
AddTextComponentString("Aereo Ostile")
EndTextCommandSetBlipName(blip)

table.insert(spawnedPlanes, {plane = plane, pilot = pilot, blip = blip})

-- thread per monitorare distruzione
CreateThread(function()
    while DoesEntityExist(plane) do
        Wait(2000)
        if IsEntityDead(pilot) or IsEntityDead(plane) then
            if DoesBlipExist(blip) then
                RemoveBlip(blip)
            end
            break
        end
    end
end)

end)