How to tame SetPedIntoVehicle! So much killing!


When i spawn this npc in the car, it instant death and falling on the floor

SetPedIntoVehicle(316162, GetPlayersLastVehicle(), -1)

Is there a way to stop making peds runing away after spawn, or mostly, not dying on spawn would be great.
Any input are welcome, point in the right direction, thank you very much!



Try to use CreatePedInsideVehicle



oh, im trying to code a function that get a pedestrian to climb in the car… hooker style… so i rather not “create” a new entity but i’ll test it for sure… right now



Here is a sample

			IsControlJustPressed(0, Keys['F9']) then
			print('F9 pressed! --------------------------------------------------'); --debug
			local char = GetPlayersLastVehicle()
			local MaxPassenger = GetVehicleMaxNumberOfPassengers(GetPlayersLastVehicle())
			local handle, ped = FindFirstPed() -- This function will set the closest entity to you as the variable entity.
			local finished = false -- FindNextPed will turn the first variable to false when it fails to find another ped in the index
				if not IsEntityDead(ped) then
						pedindex[ped] = {}
				finished, ped = FindNextPed(handle) -- first param returns true while entities are found
			until not finished

			for peds,_ in pairs(pedindex) do
				print('peds: ' .. peds) -- debug
				if peds ~= nil then -- target all peds in the area .... ak-47 style
					SetPedIntoVehicle(peds, GetPlayersLastVehicle(), -2)		-- Player in passenger seat


Then you have to use TaskEnterVehicle
Quick example:
TaskEnterVehicle(Ped, Vehicle, 10000, 0, 1.0, 1, false)

1 Like


Thats great news… but since this time the ped is not me but a pedestrian walking the street… how do you target the specific ped ? (for the first parameter of TaskEnterVehicle). i dont know how to select an entities like an NPC or any objects really… i need to learn that.

// 0xFB012961 
// FindFirstPed
int FIND_FIRST_PED(Entity* outEntity);

is supposed to find the closest to the player ? Is that how i should identify the right ped (here it will be, the closest to the car when you call the function)



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:



If you want it for a hookers why you are trying to pick closest ped instead of creating a ped? that would work perfectly with the task that i gave you.

1 Like


“realism” … i got my own rules on my server but it seem like cheating to just spawn what i need instead than driving around and spoting the npc you want in the city then ask him to climb in…

i got simcity games cheat code but will never use them for the same reason, since it kill the “grind” to get there… once “there” there is no more goal for me… i just stop playing…



Have you tried to use ClearPedTasks before set it into vehicle ? I think your problem is that the ped keep is task and maybe play anim don’t supposed to be played in a vehicle and cause glitchs. It already append to me with my player ped with script that don’t anticipate this kind of things and it fall though the vehicle and ground. Sometimes he became invisible without falling though and became visible again after get out of vehicle.



it did work perfectly… i had a delay in trying to code this part… I tried today and went through like a breeze… i decided also to use my own npcs… so… i had the ids while I created them.



no but its a good idea to add before asking the ped to enter with TaskEnterVehicle… for good mesure… in case the peds was doing something. i did not had the problem yet since my genereted npcs are not moving.



You finally spawn peds your self ?



You finally spawn peds your self ?
If you have a trainer or commands that gave you the ability to play upper and lower anim a the same time in a vehicle you may be able to reproduce what i say on my last post.