NPC Factions: Need better hook.

I have this NPC Factions script from garrysmod.org.


function DoRespawn(ply)
	ply:SetVar("faction",nil)
end

hook.Add("PlayerSpawn","nhmSpawnHook",DoRespawn)

function DoMove(ply,move)
	if ply:GetVar("faction") == nil then
		mdl = string.sub(ply:GetModel(),15,-5)
		local fact = ""
		if mdl=="police" || mdl == "combine_soldier" || mdl == "combine_soldier_prisonguard" || mdl == "combine_super_soldier" || mdl == "breen" then
			fact = "f_combine"
		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"
		end
		ply:SetVar("faction",fact)
		ply:SetName(fact)
	end
end

hook.Add("SetupMove","nhmMoveHook",DoMove)

function SpawnedNPC(ply,npc)
	cl = npc:GetClass()
	if cl == "npc_metropolice" || cl == "npc_mortarsynth" || cl == "npc_turret_ceiling" || cl == "npc_stalker" || cl == "npc_combine_camera" || cl == "monster_apc" || cl == "npc_sniper" || cl == "npc_combinedropship" || cl == "combine_mine" || cl == "npc_combine_s" || cl == "npc_manhack" || cl == "npc_helicopter" || cl == "npc_scanner" || cl == "combine_mine" || cl == "npc_combinegunship" || cl == "npc_combinedropship" || cl == "npc_strider" || cl == "npc_rollermine" || cl == "npc_cscanner" || cl == "npc_turret_floor" || cl == "npc_hunter" then
		npc:Fire("setrelationship","f_human d_ht 99",0)
		npc:Fire("setrelationship","f_zombie d_ht 98",0)
		npc:Fire("setrelationship","f_combine 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)
	end
end

hook.Add("PlayerSpawnedNPC","nhmNPCHook",SpawnedNPC)

It works as it should, if I am of combine model and I spawn a combine soldier from the NPCs tab of the spawn menu, he’s on my side.
Fantastic.
However, if I use any other method, say a STOOL or SWEP, the NPCs stay on their default, aka, the soldier will kill me…and kill me again…and again…
Which means I cant use my npc spawn platform stool to spawn a zombie vs combine army, or my spawn swep to spawn striders on my side.

I’m no lua coder, but I think if the function was bound to a different hook, or put on a timer, it might work with these.

Fixed so it works for all types of spawners and made it more optimized & customizable:
[lua]
local Faction = {}
– If there’s a missing model you can add it yourself to the table. Keep it in lowercase or it’ll not work!
Faction.Rebel = {
“odessa”,
“alyx”,
“eli”,
“barney”,
“gman”,
“monk”,
“mossman”,
“kleiner”,
“male_01”,
“male_02”,
“male_03”,
“male_04”,
“male_05”,
“male_06”,
“male_07”,
“male_08”,
“male_09”,
“female_01”,
“female_02”,
“female_03”,
“female_04”,
“female_06”,
“female_07”,
“breen”,

"npc_citizen",
"npc_alyx",
"npc_barney",
"npc_breen",
"npc_dog",
"npc_eli",
"npc_fisherman",
"npc_gman",
"npc_kleiner",
"npc_magnusson",
"npc_monk",
"npc_mossman",
"npc_vortigaunt"
}

Faction.Combine = {
“police”,
“combine_soldier”,
“combine_soldier_prisonguard”,
“combine_super_soldier”,
“breen”,

"npc_metropolice",
"npc_mortarsynth",
"npc_turret_ceiling",
"npc_stalker",
"npc_sniper",
"npc_combinedropship",
"npc_combinegunship",
"npc_helicopter",
"npc_combine_s",
"npc_turret_floor",
"npc_manhack",
"npc_cscanner",
"npc_rollermine",
"npc_hunter",
"npc_strider"
}

Faction.Zombie = {
“corpse1”,
“charple01”,
“classic”,

"npc_zombine",
"npc_zombie",
"npc_zombie_torso",
"npc_headcrab",
"npc_headcrab_black",
"npc_headcrab_fast",
"npc_fastzombie",
"npc_fastzombie_torso",
"npc_poisonzombie",
"npc_antlion",
"npc_antlionguard"

}

– Don’t edit things below this line unless you know what you’re doing

local function OnEntityCreated(npc)
if npc && npc:IsValid() && npc:IsNPC() then
local class = string.lower(npc:GetClass())
if table.HasValue(Faction.Combine, class) then
npc:Fire(“setrelationship”,“f_human d_ht 99”,0)
npc:Fire(“setrelationship”,“f_zombie d_ht 98”,0)
npc:Fire(“setrelationship”,“f_combine d_li 97”,0)
elseif table.HasValue(Faction.Rebel, class) 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 table.HasValue(Faction.Zombie, class) 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)
end
end
end

hook.Add(“OnEntityCreated”, “factionOnEntityCreated”, OnEntityCreated)

local function PlayerSpawn(pl)
hook.Call(“PlayerSetModel”, GAMEMODE, pl)
local mdl = pl:GetModel()
mdl = string.lower(string.sub(mdl,15,-5))
local fact = “”
if table.HasValue(Faction.Combine, mdl) then
fact = “f_combine”
elseif table.HasValue(Faction.Rebel, mdl) then
fact = “f_human”
elseif table.HasValue(Faction.Zombie, mdl) then
fact = “f_zombie”
end
pl:SetName(fact)
for k,v in pairs(ents.GetAll()) do
if v && v:IsValid() && v:IsNPC() then
OnEntityCreated(v)
end
end
end

hook.Add(“PlayerSpawn”, “factionPlayerSpawn”, PlayerSpawn)
[/lua]

Considering the idea I’ve had for a new gamemode, this script should fix the one issue I’d have. Obliged.