NPCs ignoring players in another team

Is it possible to make NPCs ignore players in TEAM_RED or whatever?
That’s the question.

If I’m right, this should ignore every player of the team you give it:
[lua]function NPCIgnoreTeamPlayers(npc, teamnum)
for k, v in pairs(team:GetPlayers(teamnum)) do
NPC:AddRelationship( v … " D_NU 99")
end
end
[/lua]

This needs to be done for each NPC that you spawn.

So just hook it to PlayerSpawnedNPC? Thats if the gamemode is derived from sandbox, though.

[lua]function NPCIgnoreTeamPlayers(ply, npc)
for k, v in pairs(player.GetAll()) do
if v:Team() == TEAM_RED then
npc:AddRelationship( v … " D_NU 99")
end
end
end

hook.Add(“PlayerSpawnedNPC”, “Hai”, NPCIgnoreTeamPlayers)[/lua]

Ok I’m gonna check this out.

[editline]01:53PM[/editline]

It doesn’t work.

Well, is there a TEAM_RED?

Yeah.

Are you manually spawning NPCs or spawning them trough code?
Jamie’s code is only applicable in the former case.

This should work:
[lua]
hook.Add(“OnEntityCreated”, “EntSpawn_ApplyNPCRelationhips”, function(ent)
if ValidEntity(ent) && ent:IsNPC() then
for k, pl in pairs(player.GetAll()) do
if pl:Team() == TEAM_RED then
ent:AddEntityRelationship(pl, D_NU, 100)
end
end
end
end)

hook.Add(“PlayerInitialSpawn”, “PlayerSpawn_ApplyNPCRelationships”, function(pl)
if pl:Team() == TEAM_RED then
for k, ent in pairs(ents.GetAll()) do
if ent:IsValid() && ent:IsNPC() then
ent:AddEntityRelationship(pl, D_NU, 100)
end
end
end
end)
[/lua]

No, I’m not spawning them.

I’ll check that out.

[editline]05:43PM[/editline]

It doesn’t work too. :confused:

Then you, my friend, are doing something wrong. Are you sure the players are on the right team, that the team really has this name? Where are you placing this code, are you sure it gets ran?

** [ b ] **
Shared.lua
[lua]GM.TeamBased = true[/lua]
[lua]/---------------------------------------------------------
Function Create Teams
---------------------------------------------------------
/
function GM:CreateTeams()
TEAM_RED = 1
team.SetUp(TEAM_RED, “Zombies”, Color(200,40,40,255) )
team.SetSpawnPoint( TEAM_RED, “info_player_terrorist” )

TEAM_BLUE = 2
team.SetUp(TEAM_BLUE, "Humans", Color(0,90,200,255) )
team.SetSpawnPoint( TEAM_BLUE, "info_player_counterterrorist" )

end[/lua]

I’m not using Zombie Survival by JetBoom.

Are you sure GM:CreateTeams() is being called? Why not just put the code in shared.lua with no GM:CreateTeams and see if that works. Is your scoreboard reflecting the changes of your teams?

The GM:CreateTeams() function is useful to get your teams in the TeamMenu at the initial spawn; **GM.TeamBased = true ** makes the TeamMenu.

[editline]12:16AM[/editline]

Oh wow, there are some news:
I just noticed that New Spawned Zombies (by map) don’t attack me when I’m in TEAM_RED.
Seems like it can’t change my friendship with other NPCs if they are not new.

[/QUOTE]

Oh wow, there are some news:
I just noticed that New Spawned Zombies (by map) don’t attack me when I’m in TEAM_RED.
Seems like it can’t change my friendship with other NPCs if they are not new.
[/QUOTE]

*Facepalm did you even read the code? It hooked to NPCSpawn Entity -.-

[lua]
hook.Add(“PlayerSpawn”, “PlayerSpawn_ApplyNPCRelationships”, function(pl)
if pl:Team() == TEAM_RED then
for k, ent in pairs(ents.GetAll()) do
if ent:IsValid() && ent:IsNPC() then
ent:AddEntityRelationship(pl, D_NU, 100)
end
end
end
end)
[/lua]

It’s not that hard to figure out.

Right… thanks.
It works now.