Setting Special NPC Relationships

I am attempting to make NPC’s not attack the owner, but attack other players, and failing miserably. This mod is running on darkRP

Using the line:

npc:Fire("setrelationship","f_human d_ht 98",0)

I made it so they will attack players, but cannot seem to be able to filter out the owner. I have tried this:

		makenpc:SetPos( self.Entity:GetPos() )
		makenpc:Fire("setrelationship","player d_ht 98",0)
		makenpc:Fire("setrelationship",""..makenpc.Owner:GetName().." d_li 97",0)

(its “makenpc” because thats from a different file, its correct)
I have also tried this; which is the public faction mod totally redone:

function DoRespawn(ply)


function DoMove(ply,move)
		local t = string.sub(ply:Team(),1,-1)
	if ply:GetVar("faction") == nil then

		local fact = ""
		if t == "1" then
			fact = "f_citizen"
		--elseif mdl == "alyx" || mdl == "barney" || mdl == "eli" || mdl == "female_04" || mdl == "female_06" || mdl == "female_07" || mdl == "gman_high" || mdl == "Kleiner" || mdl == "male_02" || mdl == "male_03" || mdl == "male_08" || mdl == "monk" || mdl == "mossman" || mdl == "odessa" then
		--	fact = "f_human"
--		elseif mdl == "corpse1" || mdl == "charple01" || mdl == "classic" then
	--		fact = "f_zombie"


function SpawnedNPC(ply,npc)
	local own = npc.Owner
	if ply:Team() == "1" then
--		npc:Fire("setrelationship","f_human d_ht 99",0)
	--	npc:Fire("setrelationship","f_zombie d_ht 98",0)
		npc:Fire("setrelationship","f_citizen d_li 97",0)
--	elseif cl == "npc_alyx" || cl == "npc_kleiner" || cl == "npc_eli" || cl == "npc_barney" || cl == "npc_citizen" || cl == "npc_mossman" || cl == "npc_monk" || cl == "npc_vortiguant" || cl == "npc_dog" then
--		npc:Fire("setrelationship","f_combine d_ht 99",0)
--		npc:Fire("setrelationship","f_zombie d_ht 98",0)
--		npc:Fire("setrelationship","f_human d_li 97",0)
--	elseif cl == "npc_zombie" || cl == "npc_fastzombie" || cl == "npc_poisonzombie" || cl == "npc_zombie_torso" || cl == "npc_fastzombie_torso" || cl == "npc_headcrab" || cl == "npc_headcrab_fast" || cl == "npc_headcrab_black" then
--		npc:Fire("setrelationship","f_combine d_ht 99",0)
--		npc:Fire("setrelationship","f_human d_ht 98",0)
--		npc:Fire("setrelationship","f_zombie d_li 97",0)


The following snip from above returns “1” in chat if I am a citizen, so if it does return 1, I have it set to make my name “f_citizen” (I made that name, is that ok? the faction mod only had f_human, f_combine, and f_zombie, but I did not see anything that indicated that those names are built into gmod, but rather made up for this faction mod, suggesting I could create as many different f_* as I want)

Anyway: Ideally I would just like to learn how to make it so the NPC will not attack the owner, but if thats too difficult or impossible, I would settle with having the relationship be specific to the team.

Thanks in advanced for sharing your time and knowledge!

I scratched the “faction” idea and am now trying this:

	local makenpc = ents.Create( "npc_antlion" )
--		makenpc:Fire("setrelationship","player d_li 97",0)
		local pteam = ""
	for k,v in pairs(player.GetAll()) do
		if v == makenpc:GetOwner() then
			pteam = v:GetName()
			makenpc:AddEntityRelationship(v, D_LI, 99 )
			--makenpc:Fire("setrelationship",""..pteam.."d_li 97",0)
--	makenpc:AddEntityRelationship(k, 1, 999 )
		--makenpc:Fire("setrelationship",""..tostring(v).."d_ht 98",0)
		makenpc:SetPos( self.Entity:GetPos() )
		makenpc:Fire("setrelationship","player d_li 97",0)
		makenpc:Fire("setrelationship","npc_antlion d_ht 98",0)
		makenpc:Fire("setrelationship","npc_antlionguard d_ht 98",0)
		makenpc:Fire("setrelationship","npc_antlion_worker d_ht 98",0)
		makenpc:Fire("setrelationship","npc_antlion_grub d_ht 98",0)
		makenpc:Fire("setrelationship","npc_cscanner d_ht 98",0)
		makenpc:Fire("setrelationship","npc_clawscanner d_ht 98",0)
		makenpc:Fire("setrelationship","npc_combine_s d_ht 98",0)
		makenpc:Fire("setrelationship","npc_crow d_ht 98",0)
		makenpc:Fire("setrelationship","npc_headcrab d_ht 98",0)
		makenpc:Fire("setrelationship","npc_headcrab_fast d_ht 98",0)
		makenpc:Fire("setrelationship","npc_headcrab_black d_ht 98",0)
		makenpc:Fire("setrelationship","npc_fastzombie d_ht 98",0)
		makenpc:Fire("setrelationship","npc_fastzombie_torso d_ht 98",0)
		makenpc:Fire("setrelationship","npc_zombie d_ht 98",0)
		makenpc:Fire("setrelationship","npc_zombie_torso d_ht 98",0)
		makenpc:Fire("setrelationship","npc_zombine d_ht 98",0)
		makenpc:Fire("setrelationship","npc_pigeon d_ht 98",0)
		makenpc:Fire("setrelationship","npc_crow d_ht 98",0)
		makenpc:Fire("setrelationship","npc_vortigaunt d_ht 98",0)
		makenpc:Fire("setrelationship","npc_seagull d_ht 98",0)
		makenpc:Fire("setrelationship","npc_poisonzombie d_ht 98",0)
		makenpc:Fire("setrelationship","npc_metropolice d_ht 98",0)
		makenpc:Fire("setrelationship","npc_manhack d_ht 98",0)
		makenpc:Fire("setrelationship","npc_hunter d_ht 98",0)
		makenpc:Fire("setrelationship","npc_kleiner d_ht 98",0)
		makenpc:Fire("setrelationship","npc_eli d_ht 98",0)
		makenpc:Fire("setrelationship","npc_breen d_ht 98",0)
		makenpc:Fire("setrelationship","npc_alyx d_ht 98",0)
		makenpc:Fire("setrelationship","npc_barney d_ht 98",0)
		makenpc:Fire("setrelationship","npc_dog d_ht 98",0)
		makenpc:Fire("setrelationship","npc_gman d_ht 98",0)
		makenpc:Fire("setrelationship","npc_mossman d_ht 98",0)
		makenpc:Fire("setrelationship","npc_barnacle d_ht 98",0)
		makenpc:Fire("setrelationship","npc_monk d_ht 98",0)
		makenpc:Fire("setrelationship","npc_magnusson d_ht 98",0)		

This is meant to make the npc “like” its owner (and as show, “like” other plays too, but remember in the end I want it to “hate” other players, but bear with me as I made the code like this to debug an issue I’m having)

Now When I spawn the npc without me being the owner, He will not attack me, just as the code is designed to do as shown. However, when I spawn it and I AM set to owner (verified via debug methods), He attacks me, even though I have it set too add relationship to myself as “like”. therefore, it seems this line, which is only passed if I am the owner, is doing the exact opposite of what it says:

makenpc:AddEntityRelationship(v, D_LI, 99 )

I’ve been working for hours, trying to get this to work properly, and to no avail. Please help me out guys!

local tbl = ents.GetAll()
	for k, v in pairs ( tbl ) do
	if v:IsNPC() then
	v:AddEntityRelationship(self, D_HT, 99)

That’s what I use for my SNPCs (hated by all other NPCs)… maybe it’s that you have makenpc and v the wrong way around?

My line says set the NPCs disposition to v as like, yours says set all npcs disposition to the NPC as. If i switched mine it would attempt to set my players disposition to my NPC, which will either break or do nothing. Thanks for the reply tho

Lua tags please

Still completely stumped on this guys, I’ve spent hours trying to get it right. I even added a debug line that would return the enum associated with disposition. When I create the npc, it DOES return as LIKE!(enum 3), however it will start attacking me. If I remove the line “makenpc:AddEntityRelationship(v, D_LI, 99 )” which is only called if I create the ent, he will NOT attack me? This makes absolutely no sense! How does adding a “like” disposition make the NPC hostile to me?

Here is what I have in my code, consolidated:
local makenpc = ents.Create( “npc_antlion” )
makenpc:SetPos( self.Entity:GetPos() )
makenpc:Fire(“setrelationship”,“player 0 97”,0)
makenpc:ClearEnemyMemory( )
makenpc:SetEnemy( NULL )
for k,v in pairs(player.GetAll()) do
if v == makenpc:GetOwner() then
pteam = v:GetName()
v:ChatPrint(“You are the Owner of the entity!”)
makenpc:AddEntityRelationship(v, 2, 99 )
– if v != makenpc:GetOwner() then
– makenpc:AddEntityRelationship( v, D_LI, 99 )
– v:ChatPrint(""…makenpc:Disposition(v)…"")
– end

Anyone have an idea whats causing him to attack me when I add the like disposition? or how I can override whatever is causing this behavior (most likely from base code)