Skip to content

Vehicles and peds

Vehicles

const car = server.createVehicle(411, { x: 2493, y: -1660, z: 13.3 }, 90, {
  colors: [1, 0],          // carcols.dat palette, 0-126
  plate: "SAGL 01",
  radio: RadioStation.RadioLosSantos,
  respawnDelay: 60,        // back where it was, a minute after it's wrecked or abandoned
  world: 0,
});

Whoever drives a vehicle simulates it and syncs it to everyone; empty, it stays where it was left. The server keeps its health, damage, colours, plate and radio:

car.health = 1000;
car.repair();
car.colors = [3, 3];
car.plate = "NEW PLT";
car.damage = { ...car.damage, tyres: [TyreDamage.Burst, 0, 0, 0] };
car.explode(player);       // credited to player
car.respawn();
car.destroy();

Events: playerEnterVehicle, playerExitVehicle, vehicleHit (anything that hits it: crashes into vehicles, players and peds, objects and walls, and bullets, fire and explosions; return false to undo one), vehicleDeath (return false to save it), vehicleRespawn, vehicleRadio.

server.on("vehicleHit", (vehicle, hit) => {
  if (hit.with === "ped" && hit.player && vehicle.driver) {
    server.broadcast(`${vehicle.driver.name} ran over ${hit.player.name}!`);
  }
});
server.on("vehicleDeath", (vehicle, killer) => {
  if (vehicle.data.indestructible) return false;   // repaired instead
});

Drive-bys

Drivers lean out and shoot as in the game: look left or right (Q/E, or L2/R2) holding a sub-machine gun, and fire. Passengers can too, which the game alone doesn't allow (only in its two-player mode): look left or right in a seat that can lean out (cars and bikes, not helicopters, planes or boats), aim, fire; looking ahead again ends it. Everyone sees them lean out and their shots, which count as any other's.

Peds

Server peds are NPCs everyone sees the same. The nearest player's game runs each one's AI as in single-player: it flees, fights back, falls, dies.

const guard = server.createPed(280, { x: 1550, y: -1675, z: 16.2 }, 90, {
  weapon: Weapon.Pistol,
  health: 150,
  behaviour: "stand",      // or "wander"
});

const taxi = server.createVehicle(420, { x: 1540, y: -1680, z: 13.5 });
const driver = server.createPed(7, taxi.position);
driver.putInVehicle(taxi, 0);   // a "wander" driver drives the roads

Peds' hits on players and other peds go through the server too: playerDamage and pedDamage get a Player or a Ped as attacker.

server.on("pedDeath", (ped, killer) => {
  if (killer instanceof Player) killer.giveMoney(50);
});

pedViolation reports when the game simulating a ped claimed something impossible; after a few, another player's game takes it over.

Full reference: Vehicle, Ped.