The information regarding to how to create Ped(s) that will patrol between points are either limited or confusing. Here’s an example on spawn multiple ped ( depend on how many patrol points ) and attack player if one of them spotted the player. Not using TaskCombatPed due to the ped will only attack one player. Below client-sided code.
local PedAlertThread = false
local listofPed, spawnedPeds = {"g_m_y_korean_01", "g_m_y_korean_02"}, {}
local patrolPoints = {
[1] = {
vector3(-1541.6444, -3238.3672, 28.6342),
vector3(-1528.7126, -3216.1829, 28.6342)
},
[2] = {
vector3(-1564.4523, -3229.4038, 28.6341)
},
[3] = {
vector3(-1580.3143, -3218.1042, 28.6341),
vector3(-1585.9856, -3227.3601, 28.6341)
}
}
RegisterCommand("testped", function(source, args, rawCommand)
local pedModel = listofPed[math.random(1, #listofPed)]
RequestModel(pedModel)
while not HasModelLoaded(pedModel) do
Wait(1)
end
AddRelationshipGroup("md_m")
SetRelationshipBetweenGroups(5, GetHashKey("md_m"), GetHashKey("PLAYER"))
SetRelationshipBetweenGroups(5, GetHashKey("PLAYER"), GetHashKey("md_m"))
for index, data in ipairs(patrolPoints) do
local ped = CreatePed(4, GetHashKey(pedModel), data[1].x, data[1].y, data[1].z, 0.0, true, false)
SetPedRelationshipGroupHash(ped, GetHashKey("md_m"))
SetPedFleeAttributes(ped, 0, 0)
SetPedCombatAttributes(ped, 0, false)
SetPedCombatAttributes(ped, 5, true)
SetPedCombatAttributes(ped, 16, true)
SetPedCombatAttributes(ped, 46, true)
SetPedCombatAttributes(ped, 26, true)
SetPedCombatAttributes(ped, 2, true)
SetPedCombatAttributes(ped, 1, true)
SetPedCombatAttributes(ped, 3, false)
SetPedCombatAttributes(ped, 52, true)
SetPedCombatAttributes(ped, 0, true)
SetPedCombatAttributes(ped, 20, true)
SetPedSeeingRange(ped, 50.0)
SetPedHearingRange(ped, 100.0)
SetPedCombatRange(ped, 1)
SetPedCombatAbility(ped, 100)
GiveWeaponToPed(ped, GetHashKey("weapon_carbinerifle_mk2"), 250, false, true)
SetPedAsEnemy(ped, true)
table.insert(spawnedPeds, {patrolIndex = index, thisPed = ped, currentPoint = ((#data ~= 1) and 1 or 0)})
end
SetModelAsNoLongerNeeded(GetHashKey(pedModel))
PedAlertThread = true
Citizen.CreateThread(function()
while PedAlertThread do
Citizen.Wait(1)
for i, pD in ipairs(spawnedPeds) do
if GetPedAlertness(pD.thisPed) ~= 0 then
for ii, zpedData in ipairs(spawnedPeds) do
TaskCombatHatedTargetsAroundPed(zpedData.thisPed, 100.0, 0)
end
PedAlertThread = false
end
end
end
end)
Citizen.CreateThread(function()
while #spawnedPeds ~= 0 do
if #spawnedPeds <= 1 then
Citizen.Wait(1000)
else
Citizen.Wait(math.random(10000, 15000))
end
for i, pedData in ipairs(spawnedPeds) do
if DoesEntityExist(pedData.thisPed) and GetEntityHealth(pedData.thisPed) ~= 0 then
if pedData.currentPoint ~= 0 then
local currentPoint = pedData.currentPoint
local pedCoords = GetEntityCoords(pedData.thisPed)
local nextPoint = currentPoint % #patrolPoints[pedData.patrolIndex] + 1
local destination = patrolPoints[pedData.patrolIndex][nextPoint]
if GetDistanceBetweenCoords(pedCoords, destination.x, destination.y, destination.z, true) > 2.0 then
pedData.currentPoint = nextPoint
TaskGoStraightToCoord(pedData.thisPed, destination.x, destination.y, destination.z, 1.0, -1, 0.0, 0.0)
end
end
else
SetPedAsNoLongerNeeded(pedData.thisPed)
table.remove(spawnedPeds, i)
end
end
end
end)
end, false)