Peds attack players



I try to make a script that make peds attack players, but I don’t understand where is my mistake:

local nerbyPeds2 = GetPedNearbyPeds(GetPlayerPed(-1), -1)

	while true do
		TaskCombatPed(nerbyPeds2, GetPlayerPed(-1), 0, 16 )

Someont can tell what wrong?



I would be interested in seeing how this is done. It would have to be set so that not only does it attack the player, but all other players in the session.

Following this thread.


Last time I tried a similar native (GetPedNearbyVehicles), it returned a boolean. I think this one does too, so it won’t work.



local relationshipTypes = {


    while true do

        for _, group in ipairs(relationshipTypes) do
            -- not sure about argument order, players don't have AI so only one of these should be needed
            SetRelationshipBetweenGroups(RELATIONSHIP_HATE, GetHashKey('PLAYER'), GetHashKey(group))
            SetRelationshipBetweenGroups(RELATIONSHIP_HATE, GetHashKey(group), GetHashKey('PLAYER'))

Untested, not sure if this code even compiles or doesn’t error out, but this should work in theory, unless peds are set to use decision makers that don’t cause hated targets to be attacked, or network players aren’t a relationship group anymore.

Normally you should only ever need to set CIVMALE/CIVFEMALE and various gangs (the GTA:SA ‘riot mode’ cheat code for instance overwrote CIVMALE and CIVFEMALE relation to hate every other group), but setting all of them also works.


GetPedsNearbyPeds returns the number of peds in the table/array passed to the function. A table/array must be created.

local pedTable = {}
local number = GetPedNearbyPeds(GetPlayerPed(-1), pedTable, -1)

The peds are in PedTable you can now iterate through each Ped.

for i=1, #pedTable do
  local thisPed = pedTable[i]


Thanks for your answer.

For now, I just tested that:

	while true do
		local pedTable = {}    -- New array/table
		local playerPed = GetPlayerPed(-1) -- Give a shorter name to player
		local number = GetPedNearbyPeds(playerPed, pedTable, -1) --Add ped in table
		for i=1, #pedTable do
			local thisPed = pedTable[i]
			TaskCower(thisPed, 5000)

And that not work…

I try the code privided by Uranium and it work but some peds run away and other try to fight me (only men)… it’s funny but I want all peds to be affected (and not only attack the player but also burn or explode)

If my code is good now, I think this native does not work…

I perfom other test, and now, for this code:

local pedTable = {}
local plyPed = GetPlayerPed(-1)
local number = GetPedNearbyPeds(plyPed, pedTable, -1)

	-- body

for i = 1, #pedTable do
	if pedTable ~= nil and pedTable[i] ~= nil and DoesEntityExist(pedTable[i]) then
		ExplodePedHead(pedTable[i], 0x5FC3C11)
		Citizen.Trace("No Ped in pedTable\n")

I have this error:

[499373812] Error loading script attaque2.lua in resource weather: Invalid Lua type in __data
stack traceback:
[C]: in upvalue '_in’
citizen:/scripting/lua/natives.lua:9927: in function 'GetPedNearbyPeds’
attaque2.lua:3: in main chunk
[499373812] Failed to load client script attaquezombie2.lua.

I don’t understand this error!

Thank you!

EDIT: I have this error for the first code I have put in this post too… and when I start trying, I don’t have this error…


How would that look in C#? @nynjardin

Trying to keep my scripting in C#



I don’t know! I’m a very beginner with LUA and it’s the only language that I learning for now…


GetPedsNearbyPeds is not supported in Cfx. There’s a reason @uranium posted a snippet that does not rely on getting a pedestrian array, as that wouldn’t even work in a networked game at all.

Take Ped NPC Nearby Player

What do you mean by peds ‘burning and exploding’? If they don’t have weapons, they can’t do such things, obviously.

Also - if this only happens ‘for males’ try changing the for loop in the example?


Why do you need to ask in every single topic “how does this work in C#???”

If you’re using C#, you’re already using a feature that has only been documented for less than a week… of course nobody will be able to tell you how this works, and you’ll have to figure out the mapping yourself.


I just figure it should notate which language I am using to aid people in answering me more accurately. I don’t want any assumptions and there are threads elsewhere that give C# tutorials, so it is my belief that I am behind.


In my edited post, you can see my other script that try to make head of ped explode… in this case it’s for seeing if that work more quickly.

Ok, I hoped that can be possible. But, to be sure, you tell that be never possible?

EDIT: Is it possible to create something that attack player? I ask before passing 3 day to try coding it :wink:


It’s Lua :slight_smile: It’s no a acronym!


Anyone figure this out yet?