Scripting/Server/NPCs
An NPC is a pedestrian controlled by your script. An NPC handle is a Player instance, so every Player property and method works on it, plus the NPC-only members below. The ped is a real world ped driven by real game opcodes, so walking, pathing, animations, aiming and shooting are native game behaviour.
Creating, finding and removing
| Call | Notes |
|---|---|
CreateNPC(skin, Vector(x,y,z), angle) | Returns the NPC handle or null. Angle in degrees (0 = +X, 90 = +Y). |
CreateNPC(skin, x, y, z, angle) | Same with flat arguments. |
FindNPC(id) | id is npc.NPCID (0-63) or npc.ID (the entity slot). |
DestroyNPC(npc) / npc.Remove() | Remove it everywhere; frees the slot. |
npc.Respawn() | Back to the creation spot, full health, idle. Fires onNPCRespawn. |
skin is 0-300 (200-299 are the custom skins from <skin> in Script.xml). Skin 0 is always safe. An unloaded model falls back to ped 0 and switches when it loads. The pool holds 64 NPCs; CreateNPC returns null when full.
NPC members
| Property | Type | Access | Description |
|---|---|---|---|
NPCID | integer | r | 0-based NPC index that FindNPC takes. |
ID | integer | r | Entity slot (255 and up). Same field that player.ID uses. |
IsNPC | boolean | r | Always true. |
Skin / Health / Armour | int / float / float | rw | Health and armour 0-100. Damage from the world is reported back and tracked. |
Pos / Angle | Vector / float | rw | Reading gives the live reported position. Writing teleports. |
Weapon | integer | rw | Assigning gives the weapon with 0 ammo, and a gun with 0 rounds cannot fire (an attack objective turns into a punch). Always use npc.SetWeapon(id, ammo) instead. |
Immune | boolean | rw | See the caveat below. |
Objective | integer | rw | Current objective (OBJ_*). |
Vehicle | Vehicle|null | rw | Assign a vehicle to seat the NPC as driver; null to step out. |
VehicleSeat | integer | rw | 0 driver, 1-8 passenger, -1 out. |
| Objective | Argument | Behaviour |
|---|---|---|
OBJ_IDLE | (none) | Stand on foot. |
OBJ_WALK_TO | Vector | Walk to the point. |
OBJ_RUN_TO | Vector | Run to the point. |
OBJ_ATTACK_PLAYER | player | Attack that player (needs a weapon with ammo). |
OBJ_FOLLOW_PLAYER | player | Follow that player. |
OBJ_DRIVE_TO | use DriveTo() | Drive a car to a point. |
- Objectives are fire and forget; there is no completion event. To chain "run there, then come back", use a timer or check
npc.Posyourself. - Walk/run re-issue only when the target point actually changes; attack/follow/drive re-aim about once a second.
- An NPC inside a vehicle ignores on-foot objectives.
- The AI runs on each player's game, so two players may see a slightly different route. Positions and health are reported back so the server model converges, but the visuals are not re-synchronised.
Careful
npc.Immune is enforced by pinning health to the server value each frame and forcing the ped back to idle. An immune NPC will not die and will not stay on the ground, but it can still visibly stagger from explosions or shotgun blasts. It is not engine-level invincibility.local guard = CreateNPC(0, Vector(812.0, -935.0, 36.0), 90.0);
guard.SetWeapon(WEP_AK47, 500); // a weapon with ammo (weapon ids 0-12)
guard.SetObjective(OBJ_ATTACK_PLAYER, FindPlayer(0));
function onNPCDeath(npc, killerPlayerId)
{
if (killerPlayerId >= 0) Message(FindPlayer(killerPlayerId).Name + " took out a guard");
NewTimer("RespawnNpc", 5000, 1, npc);
}
function RespawnNpc(npc) { npc.Respawn(); }- npc.SetObjective – Starts a behaviour.
- npc.DriveTo – Puts the NPC in the driver's seat and lets the game's car AI drive to the point (steering, speed and route are native).
- npc.EnterVehicle / npc.LeaveVehicle – (v0.3.3-r4+) The NPC walks to the vehicle, opens a door and gets in as a passenger (EnterVehicle), or steps out of the vehicle it is in (LeaveVehicle)...
- npc.SetDriveTo – Self-driving for an NPC that already sits in a vehicle as DRIVER (npc.Vehicle / VehicleSeat 0).
- npc.SetCruiseSpeed – Cruise speed while driving (game units, ~10 slow, ~30 fast).
- npc.SetDrivingStyle – DRIVINGSTYLE_STOP_FOR_CARS (0), DRIVINGSTYLE_SLOW_DOWN_FOR_CARS (1), DRIVINGSTYLE_AVOID_CARS (2), DRIVINGSTYLE_PLOUGH_THROUGH (3), DRIVINGSTYLE_STOP_F...
- npc.SetAnim – Plays an animation (group 0-24, id 0-249).
Categories: NPCs