Put Player in Vehicle

I’ve been looking around and cant seem to find a script to place people in the nearest car

Police System and Cops :wink:

1 Like

Not looking for any big system, just a standalone script

1 Like

You could cut the part of police system out and put it into a standalone script.

When I tried the put in vehicle part just a bit ago it didnt work, didnt put the player in the car. I had Police System all setup etc.

This snippet would spawn a Banshee at the player’s current location, and then warp the player into the newly spawned Banshee. Without more details, I’m not sure what exactly you need. local playerPed can be changed to any player’s ped, -1 just get’s your ped.

local playerPed = GetPlayerPed(-1)
local model = GetHashKey("banshee")
RequestModel(model)

while not HasModelLoaded(model) do
	Citizen.Wait(0)
end
		
local coords = GetOffsetFromEntityInWorldCoords(playerPed, 0, 5.0, 0)
local vehicle = CreateVehicle(model, coords.x, coords.y, coords.z, coords.h, true, false)
SetVehicleOnGroundProperly(vehicle)
SetModelAsNoLongerNeeded(model)
TaskWarpPedIntoVehicle(playerPed, vehicle, -1)

EDIT: Just saw you wanted the nearest car. I’ll update the code above when I get a chance.

2 Likes

Alright well to start off. Do not use nearest vehicle. You will need to use a ray. I am not currently on my computer so i cant show you the exact code.

So say i make a command and run it /seat [id]
I would do /seat 5. The 5 would be the players server id. It would then check to see if there is a vehicle infront of you (using the ray result. Have it check if the entity is a vehicle.) After that it would send a client event to id 5. Which would run a function like SeatPlayer() which would place the ped in a vehicle.

1 Like

Alrighty, here is a revised script. This example would have teleport your player into the vehicle they are looking at when they press the F6 key. I got the getVehicleInDirection function from https://github.com/DunkoUK/dunko_vrp/blob/master/vrp_basic_mission/client.lua

function getVehicleInDirection(coordFrom, coordTo)
	local rayHandle = CastRayPointToPoint(coordFrom.x, coordFrom.y, coordFrom.z, coordTo.x, coordTo.y, coordTo.z, 10, GetPlayerPed(-1), 0)
	local a, b, c, d, vehicle = GetRaycastResult(rayHandle)
	return vehicle
end

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(0)
		if IsControlJustReleased(0, 166) then -- F6 key
			local playerPed = GetPlayerPed(-1)
			local coordA = GetEntityCoords(playerPed, 1)
			local coordB = GetOffsetFromEntityInWorldCoords(playerPed, 0.0, 5.0, 0.0)
			local veh = getVehicleInDirection(coordA, coordB)
			if DoesEntityExist(veh) then
				TaskWarpPedIntoVehicle(playerPed , veh, -1)
			end
		end
	end
end)
1 Like

Hello, I really like your cript, I use the crafting system and the blackout vehicle on my server, great jobs and I was wondering …

Could you add to the script the option that live players can transport dead players, as well as insert and remove them from the nearest vehicle?

I would use it on a Roleplay server, so if there are no EMS players online, live players can transport the dead players to the hospital in their vehicles, I would appreciate your knowledge and your help.

can someone help me with a script where people can put other players in the trunk after carrying them and vise versa

1 Like