Peds attack players


#1

Hi,

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)

Citizen.CreateThread(function()
	while true do
		TaskCombatPed(nerbyPeds2, GetPlayerPed(-1), 0, 16 )
		Citizen.Wait(0)
	end
end)

Someont can tell what wrong?

Thanks


#2

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.


#3

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


#4

*sigh*…

local relationshipTypes = {
"PLAYER",
"CIVMALE",
"CIVFEMALE",
"COP",
"SECURITY_GUARD",
"PRIVATE_SECURITY",
"FIREMAN",
"GANG_1",
"GANG_2",
"GANG_9",
"GANG_10",
"AMBIENT_GANG_LOST",
"AMBIENT_GANG_MEXICAN",
"AMBIENT_GANG_FAMILY",
"AMBIENT_GANG_BALLAS",
"AMBIENT_GANG_MARABUNTE",
"AMBIENT_GANG_CULT",
"AMBIENT_GANG_SALVA",
"AMBIENT_GANG_WEICHENG",
"AMBIENT_GANG_HILLBILLY",
"DEALER",
"HATES_PLAYER",
"HEN",
"WILD_ANIMAL",
"SHARK",
"COUGAR",
"NO_RELATIONSHIP",
"SPECIAL",
"MISSION2",
"MISSION3",
"MISSION4",
"MISSION5",
"MISSION6",
"MISSION7",
"MISSION8",
"ARMY",
"GUARD_DOG",
"AGGRESSIVE_INVESTIGATE",
"MEDIC",
"CAT",
}

local RELATIONSHIP_HATE = 5

Citizen.CreateThread(function()
    while true do
        Wait(50)

        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'))
        end
    end
end)

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.


#6

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]
end

#7

Thanks for your answer.

For now, I just tested that:

Citizen.CreateThread(function()
	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)
		end
	end
end)

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)

Citizen.CreateThread(function()
	-- body


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

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…


#8

How would that look in C#? @nynjardin

Trying to keep my scripting in C#


#9

Hi,

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


#10

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
#11

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?


#12

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.


#13

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.


#14

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:


#15

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


#16

Anyone figure this out yet?