Player::putIntoVehicle
JavaScript
This method puts player into vehicle.
The vehicle must be streamed-in for this method to work as intended.
For client side head to [Player::setIntoVehicle]
Syntax
player.putIntoVehicle(vehicle, seat)
Seats:
- 0 - driver seat
- 1 - passenger seat 1
- 2 - passenger seat 2
- 3 - passenger seat 3
Example
//Server side
mp.events.addCommand("spawncar", (player, _, model = "sultanrs") => {
const position = player.position;
const vehicle = mp.vehicles.new(mp.joaat(model), position); //Creating the vehicle
player.putIntoVehicle(vehicle, 0); //we put the player into the vehicle as soon as the vehicle is streamed in.
})
Quick Snippet
Making a custom vehicle stream in handler for server side.
//Server side
mp.events.addCommand("spawncar", (player, _) => {
const position = new mp.Vector3(573.07, -352.55, 43.21); //A random position to spawn the vehicle at
const vehicle = mp.vehicles.new(mp.joaat("sultanrs"), position); //Creating the vehicle
//Adding a custom method to the vehicle which will handle the stream in (will be called from the client).
vehicle.onStreamIn = (veh) => { //supports async as well
if (!player || !mp.players.exists(player)) return; //if the player is no longer available when this method is called we return here.
player.putIntoVehicle(veh, 0); //we put the player into the vehicle as soon as the vehicle is streamed in.
}
player.position = vehicle.position; //setting player position to the vehicle position
})
//Adding a event which will be called from client when the vehicle streams in
mp.events.add('server::vehicleStreamIn', async (player, remoteid) => {
const vehicle = mp.vehicles.at(remoteid);
if (!vehicle || !mp.vehicles.exists(vehicle)) return;
if (!vehicle.onStreamIn || typeof vehicle.onStreamIn === 'undefined') return;
vehicle.onStreamIn.constructor.name === 'AsyncFunction' ? await vehicle.onStreamIn(vehicle) : vehicle.onStreamIn(vehicle);
});
//Client side
mp.events.add('entityStreamIn', (entity) => {
if (entity.type === 'vehicle') {
mp.events.callRemote('server::vehicleStreamIn', entity.remoteId);
}
});