How To Give Client Control of Server Created Ped

I deleted everything from my previous post, and am updating with the solution. The only way to create an entity on server side and control it on client side is to make sure you are within about 500 distance units of it when it spawns. I figured it out by using the airport to spawn a server side plane and make it land. If you are on north side of airport the script fails because the NetID doesn’t exist on the client. If you are on the southside of the airport the plane will spawn and land as desired. Here are the code snippets you need to properly create the vehicle and ped, and then to control them on Server Side. Some people may already know how to do this, but I found very little thorough information on this topic so I am editing this post to hopefully help others.

ServerSide:

local planemodel = 'cuban800'
local pilotmodel = 's_m_m_pilot_02'

RegisterNetEvent('ksuie-sync:ReceiveHost', function()
    local src = source
    SyncData.Host = src
    Wait(5000)
    print("Drugplane:ReceiveHost")
    print('Line59 Spawn Plane and Pilot on Server')
    local SpawnCoords = Config.Spawn
    local local_plane = CreateVehicle(GetHashKey(planemodel), SpawnCoords.x, SpawnCoords.y, SpawnCoords.z, SpawnCoords.h, true, true, true)
    print('local_plane = '..local_plane)
    local local_pilot = CreatePed(4, GetHashKey(pilotmodel), SpawnCoords.x, SpawnCoords.y, SpawnCoords.z, SpawnCoords.h, true, true)
    print('local_pilot = '..local_pilot)
    SetPedIntoVehicle(local_pilot, local_plane, -1)
    local iteration = 1
    Wait(1000)
    SyncData = {
        Host = SyncData.Host,
        iteration = iteration,
        NetworkPlaneID = NetworkGetNetworkIdFromEntity(local_plane),
        NetworkPilotID = NetworkGetNetworkIdFromEntity(local_pilot),
    }

    QBCore.Debug(SyncData)
    TriggerClientEvent('StartFlightPath', src, SyncData)



end)
type or paste code here

ClientSide:

RegisterNetEvent('StartFlightPath', function(SD)
    print('Line61 Received StartFlightPath')
    ClientSyncData = SD
    QBCore.Debug(ClientSyncData)
    if local_pilot == nil or local_plane == nil then
        SetNetworkIdCanMigrate(ClientSyncData.NetworkPilotID,true)
        SetNetworkIdCanMigrate(ClientSyncData.NetworkPlaneId,true)
        if ClientSyncData.NetworkPilotID and ClientSyncData.NetworkPlaneID then
            local_pilot = NetToPed(SD.NetworkPilotID)
            local_plane = NetToVeh(SD.NetworkPlaneID)
            print('NetToPed = '..local_pilot)
            print('NetToVeh = '..local_plane)
        end
    end
    local playerPed = GetPlayerPed(-1)
    local playerCoords = GetEntityCoords(playerPed)
    local closestPlane = GetClosestVehicle(playerCoords.x, playerCoords.y, playerCoords.x, 1000.0,GetHashKey(planemodel),16384)
    print('closestPlane = '..closestPlane)
    SetVehicleEngineOn(local_plane,true,true,false)
    SetVehicleJetEngineOn(local_plane,true)
    SetEntityInvincible(local_plane,true)

    local runwaystart = Config.RunwayStart
    local runwayend = Config.RunwayEnd
    local landing = true

    ClearPedTasks(local_pilot)
    TaskPlaneLand(local_pilot,local_plane,runwaystart.x,runwaystart.y,runwaystart.z,runwayend.x,runwayend.y,runwayend.z)
    SetPedKeepTask(local_pilot, true)

    while landing do
        local Altitude = GetEntityCoords(local_plane)
        if Altitude.z < 16 then
            ClearPedTasks(local_pilot)
            TaskVehicleTempAction(local_pilot, local_plane, 27, -1)
            SetVehicleHandbrake(local_plane, true)
            local stopping = true
            while stopping do
                Wait(3)
                TaskVehicleTempAction(local_pilot, local_plane, 27, -1)
                SetVehicleHandbrake(local_plane, true)
                if GetEntitySpeed(local_plane) <= 2.0 then
                    print('ON GROUND')
                    landing = false
                    stopping = false
                end
            end
        end
        Wait(300)
    end
    CurrentClientTask = 'StartTaxi'
    CurrentServerTask = 'StartTaxiSequence'
    ClientSyncData = {
        Host = SD.Host,
        iteration = iteration,
        CurrentClientTask = CurrentClientTask,
        CurrentClientTask2 = nil,
        CurrentServerTask = CurrentServerTask,
        CurrentServerTask2 = nil,
        NetworkPlaneID = SD.NetworkPilotID,
        NetworkPilotID = SD.NetworkPlaneID,
        NetworkCurrentKeyHolder = nil
    }
    QBCore.Debug(ClientSyncData)
    TriggerEvent('StartTaxi',ClientSyncData)
end)

The Important bits that you have to do properly and in the right order (make sure you add a wait to give entity time to populate on the client):
Server:
NetworkPlaneID = NetworkGetNetworkIdFromEntity(local_plane),
NetworkPilotID = NetworkGetNetworkIdFromEntity(local_pilot),

Client:
SetNetworkIdCanMigrate(ClientSyncData.NetworkPilotID,true)
SetNetworkIdCanMigrate(ClientSyncData.NetworkPlaneId,true)
local_pilot = NetToPed(SD.NetworkPilotID)
local_plane = NetToVeh(SD.NetworkPlaneID)

If you are too far away the client commands will error Entity Does Not Exist If you get that error move closer to the spawn point. My plan is to put a target area players have to go to in order to initiate the scenario to ensure they are within spawn distance. Hope this helps others, it took me a longer time than I would like to discuss to figure it out.

2 Likes