[REQUEST] Move nearest player into nearest car

How can I move nearest player into nearest car?

For example a cop can do /putIn and the nearest person gets put into the backseat of the nearest vehicle

I already do this on Cops_FiveM, try to look at it to make your command :slight_smile:

That is exactly what I did… Here is the code I used. Unfortuently It doesn’t work:

server.lua

AddEventHandler('chatMessage', function(source, name, msg)
  sm = stringsplit(msg, " ");
  if sm[1] == "/put" then
    CancelEvent()
    TriggerClientEvent('putInVeh', source)
  end
end)

function stringsplit(self, delimiter)
	local a = self:Split(delimiter)
	local t = {}

	for i = 0, #a - 1 do
		table.insert(t, a[i])
	end

	return t
end

client.lua

RegisterNetEvent( 'putInVeh' )
AddEventHandler('putInVeh', function ()
  local pos = GetEntityCoords(GetPlayerPed(-1),true)
  local veh = GetClosestVehicle(pos.x,pos.y,pos.z,100.00,0)
  if IsEntityAVehicle(veh) then
    for i=1,math.max(GetVehicleMaxNumberOfPassengers(veh),3) do
      if IsVehicleSeatFree(veh,i) then
        SetPedIntoVehicle(GetClosestPlayer(),veh,i)
      end
    end
  end
end)


function GetClosestPlayer()
	local players = GetPlayers()
	local closestDistance = -1
	local closestPlayer = -1
	local ply = GetPlayerPed(-1)
	local plyCoords = GetEntityCoords(ply, 0)

	for index,value in ipairs(players) do
		local target = GetPlayerPed(value)
		if(target ~= ply) then
			local targetCoords = GetEntityCoords(GetPlayerPed(value), 0)
			local distance = GetDistanceBetweenCoords(targetCoords["x"], targetCoords["y"], targetCoords["z"], plyCoords["x"], plyCoords["y"], plyCoords["z"], true)
			if(closestDistance == -1 or closestDistance > distance) then
				closestPlayer = value
				closestDistance = distance
			end
		end
	end

	return closestPlayer, closestDistance
end

I’m not using GetClosestVehicle because it doesn’t work with some vehicle like cops vehicles.
You should use Raycast

Do you mind making that function for me?

Take this from Cops, it already working on it

function getNearestVeh()
local pos = GetEntityCoords(GetPlayerPed(-1))
		local entityWorld = GetOffsetFromEntityInWorldCoords(GetPlayerPed(-1), 0.0, 20.0, 0.0)

		local rayHandle = CastRayPointToPoint(pos.x, pos.y, pos.z, entityWorld.x, entityWorld.y, entityWorld.z, 10, GetPlayerPed(-1), 0)
		local _, _, _, _, vehicleHandle = GetRaycastResult(rayHandle)
return vehicleHandle
end

would this work?

it should work yes, try and modify if it isn’t

Which number in that would be the radius?

it isn’t a radius but the distance in front of you (like a laser). And it is the value : 20.0

Did you get this to work?

/putin ?