Hello everyone, I need help with this script.
The purpose of the script is to spawn npc and when the player approaches the npc, it makes an animation.
The code works but when we have more than 1 npc, it no longer works, the spawn of npc work but no longer do any actions.
for example.
When we have 1 npc spawned:
- Everything goes well, it does its actions in the approach of the player and when it goes away.
when we have more than 1 npc spawned:
- The npc normally appears but does not perform his basic animation, and doesn’t react to the player’s approach.
- Same when the player goes away.
code here:
– local params
local generalPed
local generalLoaded = false
local PlayingAnim = false
– position for spawning NPC
local general_peds = {
{x=-2511.16479492188, y=3616.90478515625, z=13.6422147750854, a=245.000457763672},
{x=24.392505645752, y=-1345.41369628906, z=29.4970207214355, a=264.900115966797},
}
– spawning NPC here
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)
if (not generalLoaded) then
for k,v in ipairs(general_peds) do
RequestModel(GetHashKey("mp_m_shopkeep_01"))
while not HasModelLoaded(GetHashKey("mp_m_shopkeep_01")) do
Wait(1)
end
generalPed = CreatePed(2, "mp_m_shopkeep_01", v.x, v.y, v.z, v.a, false, false)
SetPedFleeAttributes(generalPed, 0, 0)
end
generalLoaded = true
end
end
end)
– Near NPC function
function IsNearNPC()
local ply = GetPlayerPed(-1)
local plyCoords = GetEntityCoords(ply, 0)
for _, item in pairs(general_peds) do
local distance = GetDistanceBetweenCoords(item.x, item.y, item.z, plyCoords["x"], plyCoords["y"], plyCoords["z"], true)
if(distance < 6) then
return true
end
end
end
– do TaskPlayAnim (work only when just have 1 NPC)
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)
RequestAnimDict("random@shop_gunstore")
while (not HasAnimDictLoaded("random@shop_gunstore")) do
Citizen.Wait(0)
end
if (IsNearNPC()) then
if (PlayingAnim == false) then
TaskPlayAnim(generalPed,"random@shop_gunstore","_greeting", 1.0, -1.0, 4000, 0, 1, true, true, true)
PlayingAnim = true
Citizen.Wait(4000)
else
TaskPlayAnim(generalPed,"random@shop_gunstore","_idle_b", 1.0, -1.0, -1, 0, 1, true, true, true)
end
else
TaskPlayAnim(generalPed,"random@shop_gunstore","_idle_b", 1.0, -1.0, -1, 0, 1, true, true, true)
PlayingAnim = false
end
end
end)
I want every npc to perform their action but I don’t know where the problem comes from.
thanks for your help.