Hey there,
currently trying to find a way to get NPCs to act while far away from a player.
My script basically should work like this:
-- Generates Random Coordinates somewhere on the island
local RandomX = math.random(-3250, 3850) +0.1
local RandomY = math.random(-3350, 7050)+0.1
local RandomZ = GetHeightMapTopZForPosition(RandomX, RandomY)
-- Gets the closest road for vehicles to spawn on
local VehicleSpawn, Heading = GetClosestVehicleNodeWithHeading(RandomX, RandomY, RandomZ, 1.0, 1, false)
-- Load the models to spawn
local pedmodel = GetHashKey('ig_bankman')
local vehiclemodel = GetHashKey('blista')
RequestModel(pedmodel)
RequestModel(vehiclemodel)
-- two while loops to ensure the models are actually loaded before continuing
-- Create vehicle + ped
local pedveh = CreateVehicle(vehiclemodel, VehicleSpawn.x, VehicleSpawn.y, VehicleSpawn.z, Heading, true, false)
local ped = CreatePed(26, pedmodel, VehicleSpawn.x, VehicleSpawn.y, VehicleSpawn.z + 2.0, Heading, true, true)
-- set ped into vehicle and start the car
SetPedIntoVehicle(ped, pedveh, -1)
SetVehicleFixed(pedveh)
SetVehicleOnGroundProperly(pedveh)
SetBlockingOfNonTemporaryEvents(ped, true)
-- Set Target Coordinates Goal.x, Goal.y, Goal.z
-- Let the car move
TaskVehicleDriveToCoordLongrange(ped, pedveh, Goal.x, Goal.y, Goal.z, 17.0, 786603, 2.0)
The vehicle spawns without any problem at the correct position however it will only start to move once I get near it.
A VERY similar script works fine. The only real difference for that script is that I do not randomly generate the coordinates but instead have fixed coordinates to spawn the vehicles at.
Any idea on this? The car is spawned on a street facing the right direction (like 90% of the time :D). I tried using AddNavmeshRequiredRegion(VehicleSpawn.x, VehicleSpawn.y, 100.0) and ArePathNodesLoadedInArea(VehicleSpawn.x-100.0, VehicleSpawn.y-100.0, VehicleSpawn.x+100.0, VehicleSpawn.y+100.0) but no luck with these natives either.
The similar, working script can be found here: