Scripting/Server/Traffic
Synchronised ambient traffic makes the streets alive for every player. Turn it on in server.conf:
traffic_sync on
traffic_ped_density 0.25 # 0..3
traffic_car_density 0.25 # 0..3
traffic_radius 100 # players this close to a lower-id player share its traffic
server_side_traffic off # on = the server picks the simulating player by ping (see below)server_side_traffic on. GTA III's ped and car AI only exists inside a game client, so the server cannot simulate it itself. With this option the server takes over the decision of WHO runs it: the players of a group are ordered by ping and the lowest one generates and owns the group's traffic (a generator keeps the role against a 40 ms difference); an owner whose ping is high counts as farther away from every entity (0.5 m per ms above 80 ms), so a closer low-ping player takes the cars over. With off (the default) the nearest / lowest-id player does. In both modes a player at the edge of traffic_radius no longer flips between generator and viewer every second.
Traffic peds and cars are script handles named TrafficPed and TrafficVehicle. They come and go constantly. A handle whose entity is gone has Valid == false, reads as null/dead and ignores writes. Ids are reused after onTraffic*Removed, so never keep a handle past that event.
How it works: the server only relays and decides ownership; it never simulates. One player near the area runs the traffic (the "generator"); everyone else sees synchronised copies. Ownership moves to whoever is closest. Writes you make from a script are applied by the owning player's game and synced to everybody. When traffic is on, the wanted-level system is also live (police, helicopters and roadblocks at higher levels).
Global functions
| Call | Notes |
|---|---|
SetTrafficEnabled(bool) / GetTrafficEnabled() | Main world (virtual world 0) switch. Turning it off removes the traffic of world 0 only. |
ToggleTrafficEnabled(vw, bool) | Peds and cars on/off in one virtual world (0.4.3). Every world other than 0 is OFF until switched on; each world has its own population, generators and owners. Turning it off removes that world's traffic. |
ToggleTrafficPedsVW(vw, bool) / ToggleTrafficVehiclesVW(vw, bool) | Only the pedestrians / only the cars (and the peds riding in them) of that world. |
GetTrafficEnabledVW(vw) / GetTrafficPedsVW(vw) / GetTrafficVehiclesVW(vw) | Current state. Traffic handles also have a World property. |
SetTrafficDensity(ped [, car]) | 0-3. One argument sets both. |
GetTrafficPedDensity() / GetTrafficCarDensity() | |
SetTrafficRadius(metres) / GetTrafficRadius() | 20-500. |
GetTrafficPedCount() / GetTrafficVehicleCount() | |
GetTrafficPeds() / GetTrafficVehicles() | Array of handles. |
FindTrafficPed(id) / FindTrafficVehicle(id) | Handle or null. |
RemoveTrafficPed(id) / RemoveTrafficVehicle(id) | By id. true if it existed. |
ClearTraffic() | Removes everything (generators keep spawning new traffic). |
GetTrafficPedInfo(id) / GetTrafficVehicleInfo(id) | A plain table of the same data, or null. |
TrafficPed
| Property | Type | Access | Description |
|---|---|---|---|
ID | integer | r | 0-255. |
Valid | boolean | r | False once the ped is gone. |
IsTraffic | boolean | r | Always true (tells it apart from a Player). |
Model / Skin | integer | rw | Ped model 0-300. |
PedType | integer | r | Engine ped type (4/5 civilians, 6 cop, 7-15 gangs...). |
Pos | Vector | rw | Writing also takes the ped out of its car. |
Angle | float | rw | Degrees. |
Velocity | Vector | r | Game units per frame. |
Health / Armour | float | rw | Setting health to 0 kills the ped. |
Weapon | integer | rw | Assigning gives the weapon with 9999 ammo. |
State | integer | r | TRAFFIC_FOOT, TRAFFIC_ATTACK, TRAFFIC_DEAD, TRAFFIC_VEHICLE, TRAFFIC_FALL. |
Dead | boolean | r | |
Owner | Player|null | r | Player whose game runs this ped. |
World | integer | r | Virtual world the ped lives in. |
Target | Player|null | r | Player it is attacking or following. |
Vehicle | TrafficVehicle|Vehicle|null | r | Car it is in. |
Seat | integer | r | -1 on foot, 0 driver, 1-8 passenger. |
Objective, PedState | integer | r | Raw engine values. |
- ped.SetWeapon / ped.Kill / ped.Remove – Each returns true if the ped exists.
TrafficVehicle
| Property | Type | Access | Description |
|---|---|---|---|
ID / Valid / IsTraffic | integer / bool | r | |
Model | integer | r | |
Colour1 / Colour2 | integer | rw | |
Pos | Vector | rw | Teleports. |
Angle | float | rw | Degrees. |
Velocity | Vector | rw | |
Speed | float | r | km/h. |
Health | float | rw | 1000 = full. |
Wrecked / Siren / Lights | boolean | r | |
Owner | Player|null | r | |
Driver | TrafficPed|null | r | |
World | integer | r | Virtual world the vehicle lives in. |
onPlayerTakeOverTrafficVehicle), occupants go with it, and carjacking works. The converted car is deleted after 45 s with nobody in it or within 120 m.function onTrafficPedDeath(ped, killer)
{
if (killer) MessagePlayer("You killed a pedestrian", killer);
CreatePickup(1334, ped.Pos); // your own drop (traffic peds drop nothing)
}
function onPlayerCommand(player, cmd, text)
{
if (cmd == "clearpeds") foreach (p in GetTrafficPeds()) p.Kill();
}- car.Explode / car.Fix / car.Remove – Blow up / full health and no visual damage / delete.
Spawn rules (0.5.4)
Spawn rules change what the traffic is made of: more of one car or skin in an area, less of another, or none at all. The players whose games run the traffic apply them the moment the game creates a car or a ped, so everybody sees the result. Rules stay until you remove them (a script reload clears them). Up to 48 rules. The area is a rectangle x1, y1, x2, y2 (any corner order); leave it out for the whole map. Each call returns the rule id (-1 when the 48 are used up).
| Call | Notes |
|---|---|
AddTrafficVehicleSpawn(model, percent [, x1, y1, x2, y2]) | percent % of the traffic cars made in the area become model. Only ordinary traffic is replaced (police and gang cars keep their model). Custom vehicles (4000+) work too. |
AddTrafficVehicleLimit(model, percent [, x1, y1, x2, y2]) | Cars of model picked in the area are kept only percent % of the time; the game picks another car instead. 0 = this car never appears there. |
AddTrafficPedSpawn(skin, percent [, kind] [, x1, y1, x2, y2]) | percent % of the new peds of kind in the area get skin. They keep what they are: a cop with a new skin is still a cop, a gang member still fights for his gang. kind: TRAFFIC_PED_ANY (default), TRAFFIC_PED_CIVILIAN, TRAFFIC_PED_COP, TRAFFIC_PED_GANG, or one gang TRAFFIC_PED_GANG1 ... TRAFFIC_PED_GANG9. |
AddTrafficPedLimit(skin, percent [, x1, y1, x2, y2]) | Peds with skin that appear on foot in the area are kept only percent % of the time. 0 = never there. |
RemoveTrafficSpawnRule(id) | true if the rule existed. |
ClearTrafficSpawnRules() / GetTrafficSpawnRuleCount() |
function onScriptLoad()
{
// Portland Harwood: lots of Stallions, no Mr Whoopee
AddTrafficVehicleSpawn(129, 30, 800.0, -1100.0, 1200.0, -700.0);
AddTrafficVehicleLimit(113, 0, 800.0, -1100.0, 1200.0, -700.0);
// cops everywhere wear the SWAT skin half of the time
AddTrafficPedSpawn(2, 50, TRAFFIC_PED_COP);
}