I am trying to make a script which will spawn a ped to deliver your personal vehicle to you. My personal vehicle system stores vehicles persistently, so that they are always either on the map, or in a garage. When the player requests their vehicle delivered, if the vehicle is in a garage it will be spawned at the garage exit, else it must already be on the map.
I then spawn a ped in the vehicle and give them a task to move to the player every 5 seconds until they arrive, then i tell them to exit. This script works fine, however if no player is within 300-400 meters or so of the ped, their task will not begin, they only begin to move once a player goes near to the ped.
local function deliver(vehicle)
Citizen.CreateThread(function()
Citizen.Wait(1000)
local pos = GetEntityCoords(vehicle)
local ped = VRP.Ped:CreatePed("guard", pos.x, pos.y, pos.z, 0.0)
Citizen.Wait(500)
TaskEnterVehicle(ped, vehicle, 1, -1, 16, 16)
Citizen.Wait(500)
local pedID = NetworkGetNetworkIdFromEntity(ped)
local vehicleID = NetworkGetNetworkIdFromEntity(vehicle)
while(true)do
local ped = NetworkGetEntityFromNetworkId(pedID)
local vehicle = NetworkGetEntityFromNetworkId(vehicleID)
local playerPos = GetEntityCoords(GetPlayerPed(-1))
TaskVehicleDriveToCoordLongrange(ped, vehicle, playerPos.x, playerPos.y, playerPos.z, 25.0, 2883621, 10.0)
SetPedKeepTask(ped, true)
SetEntityAsMissionEntity(ped, true, true)
local vehPos = GetEntityCoords(vehicle)
local distance = GetDistanceBetweenCoords(vehPos.x, vehPos.y, vehPos.z, playerPos.x, playerPos.y, playerPos.z, true)
if(distance < 5.0)then
TaskLeaveVehicle(ped, vehicle, 64)
Citizen.Wait(1000)
TaskWanderStandard(ped, true, true)
break
end
Citizen.Wait(5000)
end
end)
end