Getting far-away peds to act?

Hey there,
question is in the title. I’d like to get peds to execute tasks while they are far away from any players.

Small definition: When talking about “vehicles” I always refer to spawning a ped inside a vehicle. “Peds” always refer to “ped on foot”.
Here’s the deal:
When spawning peds far away they won’t react at all. No matter what I tried they wouldn’t bother doing anything (except for one task I tried: TaskAimGunAtEntity() worked fine for some reason) and stand around. I tried

  • ragdolling them
  • using ActivatePhysics() on them
  • using SetEntityActivatesPhysicsAsSoonAsUnfrozen() on them
  • using FreezeEntityPosition() to freeze and unfreeze them
  • using SetEntityDynamic() on them
  • using SetEntityCollision() on them
  • using ArePathNodesLoadedInArea() as the description states this could load the path nodes in the area
  • use LoadAllPathNodes(false) [unsure if that actually did anything as the description states this was removed]
  • various combinations of NetworkRegisterEntityAsNetworked(), SetBlockingOfNonTemporaryEvents(), the last boolean of SpawnPed() / SpawnVehicle()

I am coding on my own private server and am the only player on it. This means that I should be the entity owner and allowed to do everything named above.
Spawning vehicles was VERY unreliable, working only a few times then not. This is while using the same source code. Sketch:

local FarAwayCoords = {x = FarAwayX, y = FarAwayY, z = FarAwayZ}
local vehicle = CreateVehicle( FarAwayCoords, [use proper parameters in here])
local pedInVeh = CreatePed( FarAwayCoords, [use proper parameters in here])
local pedOnFoot = CreatePed( FarAwayCoords, [use proper parameters in here])

SetPedIntoVehicle(pedInVeh, vehicle, -1)
TaskVehicleDriveWander(pedInVeh, vehicle, 17.0, 786603)
TaskWanderStandard(pedOnFoot, 10.0, 10)

This doesn’t execute most of the times for vehicles either, I pretty much tried the same as for peds.
Please note that another script I have written works absolutely perfect with no problems of the vehicles not starting to move. Big (and IMO only) difference is that the working script executes events while the non-working version is triggered in a function.


Shortly ago I had the idea to cheese the problem. The idea is to spawn peds and vehicles close to the player and then tp them away:

local FarAwayCoords = {x = FarAwayX, y = FarAwayY, z = FarAwayZ}
local playerped = GetPlayerPed(-1)
local plpedcoords = GetEntityCoords(playerped)

local pedOnFoot = CreatePed(plpedcoords, [use proper parameters here])
local pedInVeh = CreatePed(plpedcoords, [use proper parameters here])
local vehicle = CreateVeh(plpedcoords, [use proper parameters here])
SetPedIntoVehicle(pedInVeh, vehicle, -1)

TaskVehicleDriveWander(ped, vehicleInVeh, 17.0, NormalDrivingBehavior)
TaskWanderStandard(pedOnFoot, 10.0, 10)

Wait(100)

SetEntityCoords(pedOnFoot, FarAwayCoords)
SetEntityCoords(pedInVeh, FarAwayCoords)
SetEntityCoords(vehicle, FarAwayCoords)
SetPedIntoVehicle(pedInVeh, vehicle, -1)
TaskVehicleDriveWander(pedInVeh, vehicle, 17.0, NormalDrivingBehavior)
TaskWanderStandard(pedOnFoot, 10.0, 10)

Doing this I discovered the following:
It seems that vehicles will now consistently start moving and drive around after being teleported away.

Peds will walk around as long as they are near the player. Upon getting teleported away they will stop doing anything. If the player teleports (or drives) near the ped they will execute their movement task. If the player teleports away they will stop.

Any idea if the server-version or the manifest-version could have a big influence on this behavior? From the findings I’d conclude that it is not possible to get peds to act far away from the player however I don’t really believe this…

1 Like

Hey, im having some similar problem, I need to spawn a ped inside a truck far away from the player, then he needs to go to one specific location. You had a good idea with the TP thing, but they are frozen to me after the TP too, do you found any solution?

I kind of found a solution. I will need to code it though. Have started on it, currently on holidays so can’t continue coding (far away from my PC :D).
Note though that for whatever reason it works in one very specific instance. Check out the towtruck script from my resource pd5m. It’s located in Service/tow_cl.lua. That one reliably works. However the same natives (copied!) didn’t work in another context. I have no idea why…

1 Like

Hey, so coming back to leave feedback, the attempt to give the TP to entities did not work obviously, they do not start anything new away from the player, your material did not work very well with me, really, for some magic this code works hahah
But in the end, what was my idea of ​​code? Well, it is a mod for RP servers, where a faction would have the power to hack Ammu-Nation’s systems and obtain the GPS position at which a truck is going to deliver. Deliveries have a point of departure and point of arrival, and those interested in cargo should approach it before arriving, depending on the value of the cargo, it will even have an armed escort. But anyway, within that idea, I started thinking about how to make this work, and I solved it in such an idiotic way… As soon as the player is hacking, I did a FadeOut on the player screen, teleported him to the location where I will spawn the truck and the trucker and I give them the mission, and then teleport the player back to where I was and give a FadeIn. Worked perfectly.

One more note about the long distance entities of any client. They spawn if the client is around, and they don’t despawn until you mark them as NoLongerNeeded under any circumstances. The new thing: (I think) they do not freeze if the client goes too far from it, in fact they continue to do the last task assigned to then before the client leaves its range. If this task ends and there is no other client around, then they freeze

1 Like

I was making a Gruppe 6 Security Heist, had same issues with NPC not spawning and even if they spawn, they don’t get the guns etc. Even I can’t attach blips to far-distant NPC

Take a look on my last reply, one of the things that I needed was a blip for the truck. By doing the way I said I could get the blip following the truck correctly too. Remember to spawn everything you need and give all the tasks for the entities while you are near them. It obviously is a horrible work around to do this, but I think any workable work around is valid in “impossible to do” situations

The blip only works if single person is in the session but if there are more, it will show for few seconds and it will disappear away. I am trying to fix it by spawning vehicle server side and trying to attach blip maybe it works?

I’ll be following this to see what you guys come up with. I believe this has something to do with the entity migrating. I’ve tried a couple of solutions, but never quite: got it “perfect”. See this thread: Is there any way to stop OneSync from migrating entites? - #6 by deterministic_bubble.

I know one of the main things I’ve done to try and fix some of this is to never directly ask a client to do something with an entity after its created, especially if its a busy server. I put a “proxy” in place which would have the target client request the entity’s owner, then send a server request which would ask the owner to perform the function. It was very generic and pretty insecure which is why I never released it, but, the gist:

local ped  = CreatePed()
local owner = NetworkGetEntityOwner(ped)
local ownerId = GetPlayerServerId(owner)
TriggerServerEvent("nc:ped:action", PedToNet(ped), owner, "SetEntityHealth", 100)
RegisterServerEvent("nc:ped:action")
AddEventHandler("nc:ped:action", function(ped, owner, action, ...)
       TriggerClientEvent("nc:ped:action", ownerId, ped,action, ...)
end)
RegisterNetEvent("nc:ped:action")
AddEventHandler("nc:ped:action", function(ped,action, ...)
      local targetPed = NetToPed(ped)
      if not DoesEntityExist(targetPed) then return end
      if _G[action] == nil then return end
      --- super insecure
      local retVal = _G[action](targetPed, ...)
     print("Response was =>", retVal)
end)

Hey there,

so I went ahead and started trying to control entities from the serverside. The first result can be seen in this youtube video. Currently synced is spawning and despawning of entities and movement using the native TaskGoStraightToCoord() (yes, the native was recreated on the serverside). There is a whole lot that needs to be done still (e.g. making sure that entities have the same clothing on…) but I do think that this is a promising start. Opinions? :slight_smile:

1 Like

See:
https://docs.fivem.net/docs/scripting-reference/onesync/#whats-culling