ServersYour serverFor creatorsForumFAQDocumentationSecurity
Download the launcher
  1. Documentation
  2. Server functions
  3. setPedDriveRoute

Function

setPedDriveRoute

ServerPedsTrilogy Hub only

The NPC driver follows a list of points along the game's roads. The game's autopilot drives: it turns at junctions, avoids cars and waits at red lights. onPedDrivePointReached fires after each point and onPedDriveFinished after the last one. If the driver's seat is free, the NPC is put into it.

Syntax

bool setPedDriveRoute(ped ped, vehicle vehicle, table points [, table options = nil])

Arguments

pedped
The NPC.
vehiclevehicle
The vehicle.
pointstable
Points { {x, y, z}, ... } (or { x = , y = , z = }).
optionstable, optional = nil
speed (km/h, default 50), style ("normal" slows down behind cars and waits at lights, "careful" stops behind cars, "ignoreLights" overtakes and runs red lights, or a number 0–4), stopRadius (m, default 6), stopAt ({ [point index] = stop time in ms }, the vehicle holds still until the stop ends), loop (start again after the last point), lookahead (m, default 45: the game gets a target this far ahead on the route and keeps the vehicle in its lane, never past the next stop), overtakeAfter (s, default 10, 0 turns it off: after standing this long the NPC drives around the obstacle for 12 s).

Returns

true when the drive started; false if someone else is driving or the NPC is dead.

Example

local bus = createVehicle(431, 1750, -1890, 13.5)
local driver = createPed(253, 1750, -1888, 13.5)
local stops = { { 1810, -1890, 13.4 }, { 1820, -1760, 13.4 }, { 1750, -1890, 13.5 } }
setPedDriveRoute(driver, bus, stops, { speed = 45, stopAt = { [1] = 8000, [2] = 8000 }, loop = true })
addEventHandler("onPedDrivePointReached", driver, function(vehicle, index)
    outputServerLog("Autobus na przystanku " .. index)
end)

Notes

A point the vehicle has passed, or one it has not got closer to for 15 s of driving, counts as reached, so the NPC never circles a point off the road. Put points on the road (e.g. getNearestVehicleNode), not on the pavement. The drive runs in the game of the player who simulates that vehicle, and other players see it through sync. With no players nearby the server moves the vehicle straight between points, so a dense route (e.g. from getVehicleRoute) keeps it on the road in an empty city too.

See also