π§βπΎAmbient NPC
Manage and spawn custom ambient NPCs (PNJ) for your RedM server, both client-side and server-side.
β‘ Getting Started
By default, everything is already set up in config.lua
.
If you want to customize the NPC system integration, you can remove lines 3β11 in your config.lua
and use your own logic.
Start the system:
TriggerEvent("Botiv:startPNJSystem")
π¦ Client Side
Add an NPC (Event)
Create and spawn a new NPC using:
TriggerEvent("Botiv-publicnpc:insertNewNPC", "boss-1", {
model = "a_m_m_sddockworkers_02",
coords = { x = -803.6, y = -1292.98, z = 42.63 },
heading = 60.62,
anim = "WORLD_HUMAN_SIT_GROUND_READ_NEWSPAPER",
distance = 50.0,
dontreact = true,
walking = false,
walking_distance = 20.0,
weapon = ""
})
Get an NPC Entity (Export)
Retrieve the entity number of a given NPC (from your config or dynamic event):
luaCopierModifierlocal npcEntity = exports.bt_publicnpc:getNPCEntity("paper-1")
Returns: The entity ID for
"paper-1"
or0
if not loaded (player too far).
Example:
Citizen.CreateThread(function()
while true do
Citizen.Wait(100)
local botNPC = exports.bt_publicnpc:getNPCEntity("paper-1")
if DoesEntityExist(botNPC) then
if #(GetEntityCoords(PlayerPedId()) - GetEntityCoords(botNPC)) < 5.0 then
SetPedScale(botNPC, 2.0)
else
SetPedScale(botNPC, 1.0)
end
end
end
end)
π§ Server Side
Insert NPC (Server Event)
To instruct a client to spawn a new NPC from the server:
TriggerClientEvent("Botiv-publicnpc:insertNewNPC", source, "boss-1", {
model = "a_m_m_sddockworkers_02",
coords = { x = -803.6, y = -1292.98, z = 42.63 },
heading = 60.62,
anim = "WORLD_HUMAN_SIT_GROUND_READ_NEWSPAPER",
distance = 50.0,
dontreact = true,
walking = false,
walking_distance = 20.0,
weapon = ""
})
βοΈ NPC Configuration Example
Hereβs a sample from your config.lua
:
Config.PNJ = {
["paper-1"] = { -- PAPERBOY
model = "S_M_Y_NewspaperBoy_01",
coords = { x = -793.6 , y = -1292.98 , z = 42.63 },
heading = 79.56,
anim = "WORLD_HUMAN_SIT_GROUND_READ_NEWSPAPER",
distance = 50.0,
dontreact = true,
walking = true,
walking_distance = 10.0,
weapon = ""
},
-- Add more NPC configs here...
}
Key fields:
model
: Model name of the NPCcoords
: Position (x, y, z)heading
: Direction the NPC is facinganim
: Animation or scenariodistance
: Distance from which NPC is loadeddontreact
: NPC ignores world eventswalking
: NPC will walk around (boolean)walking_distance
: How far the NPC will walkweapon
: Weapon (if any) to give to the NPC
π Notes
If
getNPCEntity()
returns0
, the NPC isn't loaded for the player (usually too far away).NPCs can be managed both through config and at runtime using events/exports.
Last updated