A "headshot" code.

I was scripting around when i came over the “HitGroup()” feature. That’s when I decided to make a code that printed a message in chat if you killed someone by doing a headshot on them. This turned out to be quite hard, so can anyone help me out here?
The HitGroup for the head area is “HitGroup ( 1 )”
I was thinking something like:
[lua]
if trace.HitGroup ( 1 ) then
local ply = LocalPlayer()
ply:PrintMessage( HUD_PRINTTALK, “HEADSHOT!” )
end
[/lua]
I am really not sure on how to do this, help would be appreciated!

Try this, put it in autorun/server. [lua]hook.Add( “ScalePlayerDamage”, “HEADSHOT!”, function( pl, hitgroup, damageinfo )

if hitgroup == HITGROUP_HEAD then
	if damageinfo:GetDamage( ) >= pl:Health( ) then
		damageinfo:GetAttacker( ):ChatPrint( "HEADSHOT" );
	end
end

end );[/lua]

Hem, i am not sure, i have tried to headshot an NPC several times but nothing happens. (code is located in the gamemode’s shared.lua file) I guess they work on NPC’s too, but i doubt it since its a player code. I’ve also tried with several different NPCs just to be sure. I guess ill have to change the ScalePlayerDamage in the hook to ScaleNPCDamage.

The code i gave only works on players, change the hook to ScaleNPCDamage

Perfecto, made 2 codes, 1 for players, 1 for NPCs, works perfectly!