Peds attack players

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 Likes

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.

1 Like

*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.

4 Likes

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
1 Like

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…

How would that look in C#? @nynjardin

Trying to keep my scripting in C#

Hi,

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.

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?

I am trying to create a riot mode.
How can i get peds to hate me? Obviously GetPedsNearbyPeds doesn’t work…
And only certain peds will attack if I use that script @uranium posted.

No, GetPedNearbyPeds() doesn’t work. The valid (and probably the only) alternative is to iterate through all spawned peds by enumeration, then perform your checks.

This is the Enumerator I use.

local entityEnumerator = {
    __gc = function(enum)
        if enum.destructor and enum.handle then
            enum.destructor(enum.handle)
        end
        enum.destructor = nil
        enum.handle = nil
    end
}
  
local function EnumerateEntities(initFunc, moveFunc, disposeFunc)
    return coroutine.wrap(function()
        local iter, id = initFunc()
        if not id or id == 0 then
            disposeFunc(iter)
            return
        end
      
        local enum = {handle = iter, destructor = disposeFunc}
        setmetatable(enum, entityEnumerator)
      
        local next = true
        repeat
            coroutine.yield(id)
            next, id = moveFunc(iter)
        until not next
      
        enum.destructor, enum.handle = nil, nil
        disposeFunc(iter)
    end)
end
  
function EnumerateObjects()
    return EnumerateEntities(FindFirstObject, FindNextObject, EndFindObject)
end
  
function EnumeratePeds()
    return EnumerateEntities(FindFirstPed, FindNextPed, EndFindPed)
end
  
function EnumerateVehicles()
    return EnumerateEntities(FindFirstVehicle, FindNextVehicle, EndFindVehicle)
end
  
function EnumeratePickups()
    return EnumerateEntities(FindFirstPickup, FindNextPickup, EndFindPickup)
end

function GetAllPeds()
    local peds = {}
    for ped in EnumeratePeds() do
        if DoesEntityExist(ped) then
            table.insert(peds, ped)
        end
    end
    return peds
end

function GetAllVehicles()
    local vehicles = {}
    for vehicle in EnumerateVehicles() do
        if DoesEntityExist(vehicle) then
            table.insert(vehicles, vehicle)
        end
    end
    return vehicles
end
1 Like

I already figured it out but thanks for the alternative way :+1:

Could you share it?

I just did this and it worked:

Citizen.CreateThread(function()
    AddRelationshipGroup("rioters")
    SetRelationshipBetweenGroups(5, GetHashKey("rioters"), GetHashKey("PLAYER"))
    SetRelationshipBetweenGroups(5, GetHashKey("PLAYER"), GetHashKey("rioters"))
    SetRelationshipBetweenGroups(5, GetHashKey("rioters"), GetHashKey("rioters"))
end)