Events¶
Listeners run synchronously in the network loop. They may be async, but
where an event takes a return value (a veto or a replacement), only a
value returned synchronously counts. With several listeners, any false
wins; otherwise the last number returned does.
Players¶
playerJoin¶
A player finished joining. Everyone else has already been told.
| Parameter | Type | Description |
|---|---|---|
player |
Player |
Who joined. |
server.on("playerJoin", (player) => {
player.send(`Welcome to ${server.options.name}!`, Color.Green);
player.money = 1000;
});
playerQuit¶
A player left. The Player stops working after the listeners run.
| Parameter | Type | Description |
|---|---|---|
player |
Player |
Who left. |
reason |
string |
"quit", "timeout" or "kicked". |
server.on("playerQuit", (player, reason) => {
server.broadcast(`${player.name} left (${reason})`, Color.Grey);
(player.data.car as Vehicle | undefined)?.destroy();
});
playerSpawn¶
The player is alive in the world: after joining, and after each respawn.
| Parameter | Type | Description |
|---|---|---|
player |
Player |
Who spawned. |
server.on("playerSpawn", (player) => {
player.setPosition({ x: 2495.3, y: -1687.4, z: 13.5 }, 0);
player.giveWeapon(Weapon.Pistol, 60);
});
playerDeath¶
The player died.
| Parameter | Type | Description |
|---|---|---|
player |
Player |
Who died. |
killer |
Player | Ped | null |
Whoever last hit them within 10 seconds, if anyone. |
weapon |
number |
The Weapon that killed them. |
server.on("playerDeath", (player, killer, weapon) => {
if (killer instanceof Player && killer !== player) {
killer.giveMoney(250);
server.broadcast(`${killer.name} killed ${player.name}`);
}
});
playerDamage¶
attacker hit victim, as the attacker's game saw it, validated by the
server, before it's applied.
| Parameter | Type | Description |
|---|---|---|
victim |
Player |
Who was hit. |
attacker |
Player | Ped |
Who hit them. |
hit |
Hit |
The hit. |
Returns false to cancel the hit, or a number to replace its damage.
server.on("playerDamage", (victim, attacker, hit) => {
if (attacker instanceof Player && attacker.data.team === victim.data.team) return false;
if (hit.bodyPart === BodyPart.Head) return hit.damage * 2;
});
playerShoot¶
The player fired one round, already checked (they own the weapon, have ammo, aren't firing too fast).
| Parameter | Type | Description |
|---|---|---|
player |
Player |
Who fired. |
shot.weapon |
number |
Weapon. |
shot.origin, shot.target |
Vector3 |
Muzzle and aim point. |
Returns false to cancel: nobody else sees it and its hits don't count.
playerExplosion¶
The player's grenade, molotov, rocket or satchel went off, or a vehicle they wrecked blew up.
| Parameter | Type | Description |
|---|---|---|
player |
Player |
Whose explosion. |
explosion.type |
number |
0 grenade, 1 molotov, 2 rocket, 4 car, ... |
explosion.weapon |
number |
The Weapon, or 0 for a vehicle. |
explosion.vehicle |
Vehicle | null |
The vehicle that blew up, if one of the server's. |
explosion.position |
Vector3 |
Where. |
Returns false so its blast and fire hurt no player.
server.on("playerExplosion", (player, e) => {
if (e.weapon === Weapon.RocketLauncher && !player.data.admin) return false;
});
playerWeaponViolation¶
The player's game used or held a weapon it shouldn't. It was refused anyway; this is for logging or punishing.
| Parameter | Type | Description |
|---|---|---|
player |
Player |
Whose game. |
reason |
string |
What was wrong. |
weapon |
number |
The Weapon. |
server.on("playerWeaponViolation", (player, reason, weapon) => {
console.warn(`${player.name}: ${reason} (weapon ${weapon})`);
if (reason === "notOwned") player.kick("Weapon hacks");
});
playerWantedLevel¶
The player's game says their crimes (or losing the cops) changed their
wanted level. Only where the population's wanted is on.
| Parameter | Type | Description |
|---|---|---|
player |
Player |
Whose level. |
level |
number |
The new level, 0-6. |
old |
number |
The level before. |
Returns false to keep the old level, or a number for another.
server.on("playerWantedLevel", (player, level, old) => {
if (player.data.team === "cops") return false;
return Math.min(level, 4);
});
playerChat¶
A chat line from them (not a /command). Return false to drop it, or a
string to send instead of their text. See Chat and commands.
playerCommandText¶
A /command no registered command is called (the whole line), as SA-MP's
OnPlayerCommandText. Return true if you handled it. See
Chat and commands.
playerEnterCheckpoint, playerLeaveCheckpoint, playerEnterRaceCheckpoint, playerLeaveRaceCheckpoint¶
Into and out of their checkpoint (player.setCheckpoint)
or race checkpoint, on foot or driving.
playerAction¶
They pressed the key they have for one of the server's
actions (server.actions.register).
playerKeys¶
The buttons they hold changed, as SA-MP's OnPlayerKeyStateChange: keys
and old are Key bits (player.keys is the same
as keys). keyPressed(keys, old, key) and keyReleased(...) tell one
going down or up. What a button does depends on whether they're on foot
or in a vehicle (Key.Sprint accelerates a car). A tap between two of
their syncs still shows.
import { Key, keyPressed } from "@sagl/server";
server.on("playerKeys", (player, keys, old) => {
// Handbrake (aim on foot) stops a looping animation, as larp's.
if (keyPressed(keys, old, Key.Aim) && !player.vehicle) player.stopAnim();
});
For keys of the server's own choosing, use actions: players can rebind those.
playerCarjack¶
jacker is pulling victim out of vehicle (the server option
carjacking, on by default), as it starts. The victim's own
game drags them out; the jacker gets in once they're out, and
playerEnterVehicle follows.
playerAttachObject¶
Their client script put an object in their hands
(sagl.player.attachObject),
or took one away (model null). Return false to refuse: the others don't
see it (they do still, on their own game, until the slot is set again).
Otherwise it's theirs as if player.attachObject
had put it there.
playerInterior¶
Their game took them into another interior by itself: through one of the
game's own entrances (the yellow door markers), when the server option
gameEntrances is on. player.interior is already the new one.
playerPause¶
The player opened or closed the pause menu, or their window lost or gained focus. Their character stays in the world.
| Parameter | Type | Description |
|---|---|---|
player |
Player |
Who. |
paused |
boolean |
Pause menu open. |
focused |
boolean |
Game window focused. |
server.on("playerPause", (player, paused) => { player.data.afkSince = paused ? Date.now() : null; });
playerEnterVehicle, playerExitVehicle¶
The player got into or out of a vehicle (player.seat says where).
| Parameter | Type | Description |
|---|---|---|
player |
Player |
Who. |
vehicle |
Vehicle |
Which vehicle. |
server.on("playerEnterVehicle", (player, vehicle) => {
if (player.seat === 0 && vehicle.data.owner && vehicle.data.owner !== player.name) {
player.removeFromVehicle();
player.send("This isn't your car.");
}
});
Vehicles¶
vehicleDeath¶
The vehicle is about to blow up.
| Parameter | Type | Description |
|---|---|---|
vehicle |
Vehicle |
Which. |
killer |
Player | null |
Who wrecked it, if anyone. |
Returns false to save it: it's repaired instead.
vehicleHit¶
Something hit the vehicle: it crashed into something (another vehicle, a player or ped, an object, a wall, the ground when landing did harm), or a weapon hit it (bullets, fire, explosions). The game simulating the vehicle reports it (its driver's, or a nearby player's for an empty one), so a crash between two vehicles comes as one event for each. Hits from the same thing within 150 ms come as one. Harmless scrapes along walls aren't reported; birds don't touch cars in this game.
| Parameter | Type | Description |
|---|---|---|
vehicle |
Vehicle |
The vehicle that was hit. |
hit |
VehicleHit |
What hit it, and what it cost. |
Returns false to undo the hit (its health and dents are put back), or
a number for the health it costs instead.
// Demolition derby: only other cars do damage, and double.
server.on("vehicleHit", (vehicle, hit) => {
if (hit.cause === "collision" && hit.with !== "vehicle") return false;
if (hit.with === "vehicle") {
if (hit.player) hit.player.giveMoney(Math.round(hit.loss)); // the rammer scores
return hit.loss * 2;
}
});
vehicleRespawn¶
It was put back where it was created, new.
vehicleRespray¶
A Pay 'n' Spray or mod shop gave the player's car new colours. SA-MP's OnVehicleRespray.
vehicleMod¶
The player bought a part (a component) for their car in a mod shop. SA-MP's OnVehicleMod.
vehiclePaintjob¶
The player bought a paint job (0-2) in a mod shop. SA-MP's OnVehiclePaintjob.
vehicleFinish¶
The player bought a paint finish in a mod shop
(null: standard paint).
vehicleLivery¶
The player bought a livery in a mod shop
(null: took it off).
vehiclePlate¶
The player bought a licence plate in a mod shop.
modShop¶
The player drove into a mod shop, or out of it.
SA-MP's OnEnterExitModShop. shop is the game's shop (ModShop), or 0 for
one you made; site is the shop's id ("transfender-ls", or yours).
server.on("vehicleMod", (player, vehicle, part, price) => {
console.log(`${player.name} fitted ${part} for $${price}`);
});
vehicleRadio¶
Someone in the vehicle retuned its radio.
| Parameter | Type | Description |
|---|---|---|
player |
Player |
Who tuned it. |
vehicle |
Vehicle |
Which vehicle. |
station |
number |
The RadioStation they picked. |
Returns false to refuse, or a station to tune to instead.
server.on("vehicleRadio", (player, vehicle) => {
if (vehicle.data.police) return RadioStation.Off;
});
Peds and population¶
pedDamage¶
A ped was hit (validated like hits on players).
Returns false to cancel, or a number to replace the damage.
pedDeath¶
A ped died.
server.on("pedDeath", (ped, killer) => {
if (killer instanceof Player && ped.data.bounty) killer.giveMoney(ped.data.bounty as number);
});
missionEvent¶
Every mission start, join, leave, pass, fail and refusal, for logs and audits.
| Field | Type | Description |
|---|---|---|
type |
"start" | "join" | "leave" | "pass" | "fail" | "refused" |
|
name |
string |
The mission's name. |
mission |
Mission | null |
null for a refusal. |
player |
Player | null |
Who started, joined, left or was refused. |
reason |
string |
Why it failed or was refused. |
server.on("missionEvent", (e) => console.log(`[mission] ${e.type} ${e.name} ${e.player?.name ?? ""} ${e.reason}`));
pedOrder¶
(ped: Ped, info: { orderId: number; step: number; state: "stepDone" | "done" | "failed"; reason?: string }) => void
How a ped's order goes: a step done, the order done, or
failed (reason). Every ped's, in one place; ped.on() has the same per
ped, and the order's promise resolves with the outcome.
server.on("pedOrder", (ped, { state, reason }) => {
if (state === "failed") console.warn(`ped ${ped.id}: ${reason}`);
});
pedViolation¶
The game simulating a ped reported something impossible. The report was refused; after a few, another player's game takes the ped over.
populationSpawn¶
A player's game proposed ambient population.
| Parameter | Type | Description |
|---|---|---|
spawn.vehicle |
number | null |
Vehicle model, or null for a ped on foot. |
spawn.position |
Vector3 |
Where. |
spawn.heading |
number |
Degrees. |
spawn.peds |
{ model, pedType, weapon, seat }[] |
The ped, or the vehicle's occupants. |
player |
Player |
Whose game proposed it. |
Returns false to refuse it.
// No ambient bikes.
server.on("populationSpawn", (spawn) => {
if (spawn.vehicle !== null && [509, 481, 510].includes(spawn.vehicle)) return false;
});
Zones¶
zoneEnter, zoneLeave, zoneMove¶
The player went into a zone, came out of one, or moved inside one (each position update, about 20 a second).
server.on("zoneEnter", (player, zone) => {
if (zone.data.shop) player.emit("shop", { open: true, items: zone.data.shop });
});
server.on("zoneLeave", (player, zone) => {
if (zone.data.shop) player.emit("shop", { open: false });
});
Client scripts¶
clientMessage¶
The player's client scripts sent data (sagl.emit). Also given to that
channel's handlers (server.channel(name).on).
| Parameter | Type | Description |
|---|---|---|
player |
Player |
Whose scripts. |
channel |
string |
The channel. |
data |
unknown |
The parsed JSON. |
clientScriptError¶
A client script threw, or failed to run, in the player's game. A
screen effect's shader that doesn't compile is
reported here too, where being "shader <file>", and so is a
paint finish's ("paint <file>").
server.on("clientScriptError", (player, { where, message }) => {
console.warn(`[script] ${player.name} ${where}: ${message}`);
});
Types¶
VehicleHit¶
| Field | Type | Description |
|---|---|---|
cause |
"collision" | "weapon" |
A crash, or bullets, fire or an explosion. |
with |
"vehicle" | "ped" | "object" | "world" | "ground" | null |
What it hit or what hit it: ped is a player or a ped, object doors, fences, lamp posts..., world walls and buildings, ground only when landing did harm; null for a weapon with no owner. |
weapon |
number |
Weapon; 49 (rammed) for a crash. |
vehicle |
Vehicle | null |
The other vehicle, if it was one of the server's. |
player |
Player | null |
The player involved: the one it hit, the other vehicle's driver, or the shooter. |
ped |
Ped | null |
The server ped involved, the same way. |
healthBefore |
number |
Its health before the hit. |
health |
number |
Its health after (the same if the hit did no harm). |
loss |
number |
What the hit cost in health. |
intensity |
number |
How hard: a crash's impact as the game measures it, or a weapon's damage. |
position |
Vector3 |
Where. |
Hit¶
| Field | Type | Description |
|---|---|---|
weapon |
number |
Weapon (0 unarmed). |
bodyPart |
number |
BodyPart: 3 torso, 4 groin, 5/6 arms, 7/8 legs, 9 head. |
damage |
number |
Before armour. |
direction |
number |
Side of the victim hit, 0-3. |
lethal |
boolean |
A ped headshot that kills outright; a returned number overrides it. |