How to make a npc survive multiple headshots

I’ve found a way to make npcs not die with a single headshot in gamemodes and in sandbox.

Use this code for gamemodes:


function GM:ScaleNPCDamage( npc, hitgroup, dmginfo )

	if ( hitgroup == HITGROUP_HEAD ) then
		dmginfo:ScaleDamage(2.0)
	end

	if ( hitgroup == HITGROUP_LEFTARM ||
		hitgroup == HITGROUP_RIGHTARM || 
		hitgroup == HITGROUP_LEFTLEG ||
		hitgroup == HITGROUP_RIGHTLEG ||
		hitgroup == HITGROUP_GEAR ) then
		dmginfo:ScaleDamage( 0.5 )
	end

end

If you want to use this in sandbox, just put the below code in a new lua file in the folder ‘lua/autorun’.

Use this code for sandbox:


function HeadshotFix(npc, hitgroup, dmginfo)
	if ( hitgroup == HITGROUP_HEAD ) then
		dmginfo:SetDamage(2.0)
	end
end
hook.Add( "ScaleNPCDamage", "HeadshotFix", HeadshotFix )

Cool. Not sure where this would be particularly useful, but props for the code.

[lua]
function noHeadShotInstantKill( npc, hitgroup, dmginfo )

if ( hitgroup == HITGROUP_HEAD ) then
	dmginfo:SetDamage(1.5)
end

end
hook.Add( “ScaleNPCDamage”, “HeadshotFix”, noHeadShotInstantKill )
[/lua]
Should that not skip all the overriding, and be more “good practice”?
[lua]
function noHeadShotInstantKill( npc, hitgroup, dmginfo )

if ( hitgroup == HITGROUP_HEAD ) then
	dmginfo:SetDamage(npc:GetMaxHealth() * 0.5)
end

end
hook.Add( “ScaleNPCDamage”, “HeadshotFix”, noHeadShotInstantKill )
[/lua]
That should make the head shot lethal but still not enough to kill anyone directly, note that i have not tested this since i am at school :smiley:

I have found several good uses for scaledamage and setdamage, like i shared a small script that removes all damage from props with some random dark rp server. Now everyone only complains about prop pushing :slight_smile:

Well of course scale damage has many uses, but I mean the NPC headshots.

Whoops, I thought I had to override it to get it to work since last time I used this function, it gave errors until I overrided it. That script you gave me worked though, It was probably a command I used in my older function that gave errors. I think it was npc:GetDamage(), but I’m not sure.

I’ve just tested this, it doesn’t work. I’ve just found out that the head (the combine’s head anyway) has only five health. For example, if I set the damage to five the combine will die in a single shot, but if I set it to 4.9, it will take two shots. Your code sets the damage to half the combine’s health, so if the combine’s health if 40, the damage will be set to 20.

Also, I’ve modified my code to stop it from overriding the main function:


function HeadshotFix(npc, hitgroup, dmginfo)
	if ( hitgroup == HITGROUP_HEAD ) then
		dmginfo:SetDamage(2.0)
	end
end
hook.Add( "ScaleNPCDamage", "HeadshotFix", HeadshotFix )

Well, it would be useful for custom gamemodes that use npcs. I am also using a modified version of this code to create an addon that lets you change the difficulty to something harder then gmod’s difficulty.

Good point.

He will always be able to survive 2 head shots, if you skip shooting his legs or anything…
[lua]
function noHeadShotInstantKill( npc, hitgroup, dmginfo )
if ( hitgroup == HITGROUP_HEAD ) then
dmginfo:SetDamage(npc:Health() * 0.5)
end
end
hook.Add( “ScaleNPCDamage”, “HeadshotFix”, noHeadShotInstantKill )
[/lua]
Should make it more fun :slight_smile:

Wiki is for this kinda stuff :stuck_out_tongue: Even know this has been in the wiki for about 2 years, or more.

Link is always helpful & relevent…

Could you give me the scale how many headshots it takes for combine soldiers to survive a headshot until they die on hard mode? I remember it was noticeably over two shots to the head to kill a combine soldier.

As I said before, the npc will always die with a single headshot if the damage is set over five. So that code wouldn’t work, since the damage will always be over five unless the npc has less then ten heath.

I never saw anything like this in the wiki…

I just tested it in hard difficulty, it seems that a normal combine soldier could take as many as seven pistol shots and nine smg shots to the head. The weapons seem to be overpowered in gmod and the npcs have much less health, I’m guessing gmod is using the same settings as HL2 on easy mode.

This is awesome, I’m making a topdown gamemode and thanks to this script all of the npcs don’t die as quick.

I don’t believe that combine’s heads have 5 damage. I believe that Source scales the damage done to a combine by the head x20, assuming that a combine has 100 health.

I’ve been looking further into this subject. I recently found out that the combine has 50 health and gmod scales the damage to the head x10, so it seems I was wrong about most of this. I redone the gamemode part of this code in the op. (I changed it from SetDamage to ScaleDamage)

Awesome, I was looking for this! In a Zombie Survival gamemode this is REALLY useful!

I could not get any of these codes to work. I created a txt document and added the fuction and turned it into a lua file and put it in my lua/addons folder but i am still able to kill a npc with 1 shot to the head. I am not sure how to override but I was wondering what I am doing wrong and is there a console command that does the same thing? I found the sk_npc_head command and tweaked it but it did nothing as well. Need some help.


(User was banned for this post ("Dumb bump" - Gran PC))

lua/autorun/server

Yup still getting nothing but the same results. I put the lua files in the server folder and the addons folder and still did not work. I am using powerPC’s very first post of the lua funtions. Is that one wrong? Others posted there functions and I tried the ones that had positive feed back and nothing even then. Also tried disabling and enabling sv_cheats and got nothing as well. Pretty stumped.

go into lua/autorun/server

create a lua file named iamagaywizard.lua

[lua]
function gaywizard(npc, hitgroup, dmginfo)
if ( hitgroup == HITGROUP_HEAD ) then
dmginfo:SetDamage(2.0)
end
end
hook.Add( “ScaleNPCDamage”, “IAmGayWizard”, gaywizard )
[/lua]

paste that code into the file, make sure it’s a .lua file, save it. then go into sandbox.

Make sure not to change the name of the Lua file, it’s key.