Skip to content

Vehicle

import { Vehicle, DoorDamage, TyreDamage, PanelDamage } from "@sagl/server";

A vehicle, from server.createVehicle(), events, or server.vehicles (ambient traffic included). Whoever drives it simulates it; empty, it stays where it was left. The server keeps its health, damage, colours, plate and radio.


Properties

Name Type Access Description
id number read
model number read 400-611.
data Record<string, unknown> read Your own state.
exists boolean read False once destroyed.
position Vector3 read Last synced position.
velocity Vector3 read m/s, as last synced (still once nobody has for a second).
trailer Vehicle | null read What it tows: see Trailers.
world number read/write Virtual world; setting it moves its occupants too.
interior number read/write The interior it's in, seen only from there (as SA-MP's LinkVehicleToInterior).
driver Player | null read The player driving.
occupants Map<number, Player> read Seat → player, driver included.
peds Map<number, Ped> read Seat → server ped.
health number read/write 1000 undamaged; it burns below 250. Only the server raises it. Not for a wreck.
damage VehicleDamage read/write Its visible damage. Not for a wreck.
plate string read/write Up to 8 of A-Z, 0-9 and space.
dirt number \| null read/write How dirty it looks, 0 (clean) to 15, the same on every screen; null (the default) leaves it to each player's game, which picks one as it makes the car. Pay 'n' Sprays and new paint in a mod shop clean it.
paintFinish PaintFinish \| null read/write Its paint drawn with a shader: { type: "metallic", ... } or { shader, params }. See Paint finishes. null (the default): the game's paint.
colors [number, number] read/write Its two colours: the game's (0-127) or your own (server.vehicleColors, 128 up).
livery Livery \| null read/write An image over its paint (a sticker bomb, a paint job of your own). See Liveries.
modded boolean read/write Modified in a mod shop (set by it when anything's bought there, or by you). A Pay 'n' Spray only repairs it, keeping its paint.
radio number read/write RadioStation; -1 before anyone's been in.
respawnDelay number read/write Seconds after it's wrecked or abandoned that it respawns; 0 never.
ambient boolean read Part of the traffic.
wrecked boolean read Blown up.
const car = server.createVehicle(560, { x: 2500, y: -1670, z: 13.4 }); // Sultan
car.colors = [0, 1];
car.plate = "FAST 01";
car.radio = RadioStation.RadioX;
car.data.owner = player.name;

Hits on it (crashes, bullets, fire, explosions) fire the vehicleHit event; return false from it to undo one.


Methods

repair()

repair(): void

Full health and no visible damage, for everyone. Not for a wreck.

server.on("zoneEnter", (p, z) => { if (z === sprayZone && p.vehicle) p.vehicle.repair(); });

setPosition(position, heading?)

Puts it there at a standstill (SA-MP's SetVehiclePos, SetVehicleZAngle): position on the ground, as createVehicle takes it, heading in degrees (default as it is: vehicle.heading). Anyone in it goes too.

Locks, engine, lights, searchlight and smoke

Member
locked read/write: its doors locked for everyone; no one gets in, those in it stay.
lockFor(player, locked) For one player: true locked, false open, null as for everyone. A car lock system: car.locked = true; car.lockFor(owner, false).
engine read/write: true running, false off (it won't drive), null as the game has it (the default).
lights read/write: true on, false off, null as the game has them (on at night).
searchlight read/write, helicopters: true on, false off, null as its pilot has it (a Police Maverick's pilot switches it with the horn). Any helicopter can have one. Everyone sees it.
smoke read/write, planes: its smoke trail, true on, false off, null as its pilot has it (a Stuntplane's or Cropduster's pilot switches it). It only shows while someone flies it with the engine running.
searchlightOn, smokeOn read only: whether they're on now, as its pilot's game has them.
indicators read/write: "left", "right", "hazard" or "off", held so (its driver's keys then do nothing), or null as its driver has them (the default). Cars, bikes and trucks; they flash together on every screen.
indicatorsOn read only: what its indicators show now: held, or as its driver last had them, player or AI. They stay that way when the driver gets out. AI drivers signal their turns and use hazards when broken down; see Population.indicators.

Forcing either way takes the switch from the pilot: for a searchlight only a command turns on, force it off, and on when the command runs.

heli.searchlight = false;                    // the horn no longer switches it
server.commands.register(
  command("light").description("Your helicopter's searchlight").executes((player) => {
    const v = player.vehicle;
    if (!v) throw new CommandError("You're not in a helicopter");
    v.searchlight = !v.searchlightOn;
  }),
);

Client scripts have sagl.player.setSearchlight / setSmoke for what the local player flies, and setIndicators for what they drive; what the server forces comes first. The indicator keys and their cancelling after a turn are the server's indicators option.

car.indicators = "hazard";   // broken down: hazards, whoever drives
car.indicators = null;       // the driver's again

Tuning

Member
addComponent(part) Fits a part (1000-1193), replacing what's in its slot; false if it doesn't fit this model (the game's own carmods.dat). As SA-MP's AddVehicleComponent.
removeComponent(part) Takes it off.
components The parts on it.
componentInSlot(slot) The part in a ComponentSlot (Spoiler, Hood, Roof, SideSkirt, Lamps, Nitro, Exhaust, Wheels, Stereo, Hydraulics, FrontBumper, RearBumper, VentRight, VentLeft), or 0.
paintjob read/write: 0-2 for the models that have them, -1 none (SA-MP's 3 works too).

componentFits(model, part) and componentSlot(part) (exported) answer without a vehicle.

car.addComponent(1010);   // nitro
car.addComponent(1073);   // Shadow wheels
car.paintjob = 1;

onHit(fn), onExplosionNear(radius, fn)

onHit(fn: (hit: VehicleHit) => boolean | number | void): () => void
onExplosionNear(radius: number, fn: (player: Player, explosion: Explosion) => void): () => void

onHit: what happens to this vehicle when hit, as vehicleHit for all (called first): false refuses the harm, a number replaces what it costs. onExplosionNear: a player's grenade, molotov, rocket or satchel going off within radius m of it, before any harm. Each returns a function that removes it.

car.onHit((hit) => (hit.cause === "weapon" ? undefined : false));   // only weapons hurt it
car.onExplosionNear(8, (player) => startCutscene());

speed, isPolice, offset(by)

speed is its speed in m/s, as last synced. isPolice is whether it's one of the law's (police cars and bikes, rangers, SWAT, FBI, army). offset({ right, ahead, up }) is a point by it, by its heading: for camera shots and placing things.

player.camera.set({ position: car.offset({ right: 3, ahead: 1, up: 1 }), lookAt: { vehicle: car } });

Trailers

vehicle.attachTrailer(trailer: Vehicle): boolean
vehicle.detachTrailer(): void

Hitches trailer behind it, wherever the trailer is (SA-MP's AttachTrailerToVehicle), in every game, as backing into it would; or unhitches what it tows. Returns false if it can't: the trailer is in another world, being driven, or a wreck. Drivers hitch and unhitch them themselves too, as in the game; trailer follows either way.

const rig = server.createVehicle(403, depot, 0);
rig.attachTrailer(server.createVehicle(435, depot, 0));

explode(killer?)

explode(killer?: Player): void

Blows it up for everyone.

Parameter Type Description
killer Player Credited with it (in vehicleDeath and kills inside it).
setTimeout(() => bombCar.explode(planter), 10_000);

respawn()

respawn(): void

Puts it back where it was created, new (a wreck too).

server.console.command("respawnall", "respawnall", () => server.vehicles.forEach((v) => v.respawn()));

release()

release(): void

Hands it back to the game: the server removes it as it does ambient traffic, once nobody is near (a wreck after 30 s out of view), unless it's someone's last car.

destroy()

destroy(): void

Removes it for everyone; its occupants are put out first.

server.on("playerQuit", (p) => (p.data.car as Vehicle | undefined)?.destroy());

VehicleDamage

Field Type Description
engine number 0 fine; smoke past about 100.
tyres TyreDamage[4] Ok, Burst, Gone: front left, rear left, front right, rear right.
doors DoorDamage[6] Ok, Open, Damaged, OpenDamaged, Gone: bonnet, boot, front left, front right, rear left, rear right.
panels PanelDamage[7] Ok, Loose, Damaged, LooseDamaged: wings, windscreen, bumpers.
lights boolean[4] Smashed.
// Burst the front tyres and pop the bonnet.
const d = car.damage;
d.tyres = [TyreDamage.Burst, d.tyres[1], TyreDamage.Burst, d.tyres[3]];
d.doors[0] = DoorDamage.Open;
car.damage = d;