I dont think that find closest/first ped/vehicle/object functions works properly in MP, but I could be mistaken. Check out the enumerate function in Fivem for ped/vehicle/objects, search for "EnumeratePeds() " and you will find the function/code to use. This works in MP, and you find that there will typically be at most ~60 or so peds per client. Are you targeting ambient peds, or peds you are spawning in your resource? Eitherway, I have found the decor functions (getdecorint, setdecorint etc…) invaluable in targetting specific peds/objects/vehicles if they are mission entities or spawned by your resource.
In your case you may not need it. So in your enumerate peds function (if findfirstped does not work for you) you could do a check via Vdist or GetDistanceBetweenCoords after GetEntityCoords on the peds to find the closest ped.
If you use decor, which is a way to attach values to entities that propagate to other clients, beware that you cannot assume that every client has the entity loaded (I have seen this even when making them mission entities), so their decor will not be loaded either on every client, until the client ‘sees’ them, (typically when the client is within collision range of the entity, which sometimes can mean when you are near the ‘host’ client that spawned them). Also, the client that spawned the entity and changes the decor value, the changes will propagate to all other clients (even if eventually), but if you change the decor on the same entity with another client, you will need to make a server call, to make sure all the other clients are aware, so the ‘host’ client can make the change as well, so it propagates.
EDIT: EnumeratePeds, uses FindFirstPed so it will work and find something, but I am not sure if that is the closest ped to the player, or the first ped that spawned in the current collection, etc… so you may still need to do more checks.
also here is an example of the code to find nearest ped that works But if you want a specific ped like a hooker, it would be better to create a ped:
1 Like