Improved Headshot Script

ok… a while ago I asked the forum how to make a headshot script, it’s nice and all but I want it to only recognize headshots when a player dies, and no adding a timer that checks if the player is dead afterwards won’t do because it will make a timer every headshot, and after the player dies the killer will get like 5+ headshots if he used an automatic weapon… here is the code

hook.Add (“ScalePlayerDamage”, “lalaladoda”, function ( ply, hitgroup, dmginfo )
local ply = dmginfo:GetAttacker()
if hitgroup == HITGROUP_HEAD and hs_delay > 0 then
hs_delay = 0
ply.point = ply.point + 50
PrintMessage( HUD_PRINTTALK, ply:Nick() … " got a headshot! " … ply:Nick() … " now has " … ply.point … " points!")
PrintMessage( HUD_PRINTCENTER, ply:Nick() … " got a headshot! " … ply:Nick() … " now has " … ply.point … " points!")
timer.Simple( 0.2, function()
hs_delay = 1
end)
end
end)

SHORT VERSION: How do I make this only happen when a player dies?

PlayerDeath

[lua]hook.Add (“ScalePlayerDamage”,“HookName”,function (ply, hitgroup, dmginfo)
if hitgroup == HITGROUP_HEAD and ( dmginfo:GetDamage() >= ply:Health () ) then
–Your Stuff
end
end )[/lua]

Now, a short explanation. A player dies if the damage he has received is more than the current health he got. Making this easy and comparing the damage player received and his current health, with an additional condition of a “Headshot”, it’s simple. Isn’t it?

Not sure about how it works tho, haven’t tested it myself.

PlayerDeath and DoPlayerDeath don’t provide hitgroup info.

Insomnia Array’s example should be good.

-snip-

thanks :smiley: I think it works

Good luck with whatever you are coding.

**[Player.LastHitGroup

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Player.LastHitGroup)**

uhmm, I tried Insomnia Array’s code, but sometime it detects it sometimes it doesn’t :frowning:

[editline]8th January 2011[/editline]
[lua]
hook.Add ( “ScalePlayerDamage”, “headshotcredit”, function ( ply, hitgroup, dmginfo )
local killer = dmginfo:GetAttacker()
if hitgroup == HITGROUP_HEAD and ( dmginfo:GetDamage() >= ply:Health() ) and hs_delay > 0 then
hs_delay = 0
killer.point = killer.point + 50
killer:PrintMessage( HUD_PRINTCENTER, “HEADSHOT!”)
for k, v in pairs(player.GetAll()) do
v:EmitSound( “unreal/headshot.wav”, 100, 100 )
v:PrintMessage( HUD_PRINTTALK, killer:Nick() … " got a headshot! " … killer:Nick() … " now has " … killer.point … " points!" )
end
timer.Simple( 0.2, function()
hs_delay = 1
end)
end
end )
[/lua]

Is there anything wrong with this?

You may not be actually hitting their head if it sometimes works, are you sure that the gun you are using has little spread?

I tried with smg1, I used Faphack, and im pretty sure it always does a headshot, I tried it with teh 357 and smg, it may be the spread though, is there something weird with the classic zombie’s hitboxes?

Just throw in there a message output saying what you hit to verify you hit their head.

k, will this work better?

[lua]
hook.Add ( “PlayerDeath”, “headshotcredit”, function ( victim, weapon, killer )
if victim:LastHitGroup() == HEAD and hs_delay == 1 then
local killer = dmginfo:GetAttacker()
killer.point = killer.point + 50
killer:PrintMessage( HUD_PRINTCENTER, “HEADSHOT!”)
for k, v in pairs(player.GetAll()) do
v:EmitSound( “unreal/headshot.wav”, 100, 100 )
v:PrintMessage( HUD_PRINTTALK, killer:Nick() … " got a headshot! " … killer:Nick() … " now has " … killer.point … " points!" )
end
hs_delay = 0
timer.Simple( 0.2, function()
hs_delay = 1
end)
end
end )
[/lua]

I have tested my script - and it doesn’t work. Use what Disseminate said.

Insomnia, yours sometimes works lol, and just wondering, does the zombie have a weird head hitbox? Because I always have to hit like the tip-top of his head to get a headshot notification, I printed ply.LastHitBox, and it kept giving 1, which indicates generic…whats that? lol??? I shot the leg(forgot which one) and it gave 7, which is either the left or right leg, doesnt matter which, it worked when I shot the leg

Well, not sure about the zombie hitboxes… Afaik zombie doesn’t even have the “Head” hitgroup. Not sure, again.

Regarding my script - I have ran a test that would print “Headshot” when I do a headshot, and “Killed with headshot” when I did a headshot-kill. At least it was supposed to. When I actually loaded the script in game it would detect headshots on bots, but whenever I killed a bot with another headshot, it just printed “Headshot”, and did not print “Killed with headshot”.

Ugh, Insomnia, and Dissemate, both of your methods recognize headshots, but not properly on deaths, WHY???

bump cause I still don’t have the answer.

Player.LastHitGroup [img_thumb]http://wiki.garrysmod.com/favicon.ico[/img_thumb]

read