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.
[QUOTE=c-unit;27262724]PlayerDeath[/QUOTE]
PlayerDeath and DoPlayerDeath don't provide hitgroup info.
Insomnia Array's example should be good.
-snip-
thanks :D I think it works
Good luck with whatever you are coding.
[QUOTE=Blargh123;27263180]PlayerDeath and DoPlayerDeath don't provide hitgroup info.
Insomnia Array's example should be good.[/QUOTE]
[b][url=http://wiki.garrysmod.com/?title=Player.LastHitGroup]Player.LastHitGroup [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]
uhmm, I tried Insomnia Array's code, but sometime it detects it sometimes it doesn't :(
[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.
[QUOTE=Andriko1;27342550]bump cause I still don't have the answer.[/QUOTE]
[b][url=http://wiki.garrysmod.com/?title=Player.LastHitGroup]Player.LastHitGroup [img_thumb]http://wiki.garrysmod.com/favicon.ico[/img_thumb][/url][/b]
[quote=andriko1;27307730]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[/quote]
read
[QUOTE=Andriko1;27343704]read[/QUOTE]
It's HITGROUP_HEAD, not HEAD I am pretty sure.
Then why does it sometimes work -_-
[editline]10th January 2011[/editline]
I'll test it again with HITGROUP_HEAD
[editline]10th January 2011[/editline]
[url]http://wiki.garrysmod.com/?title=Player.LastHitGroup[/url]
read the enums
Put print(victim:LastHitGroup()) so that you can see if it is hitting the head or not. It may be something like your hitting the person again after the headshot.
I've done some small tests, and this works better
[lua]
function RegPlyHeadshots (victim, attacker, dmginfo)
if victim:LastHitGroup () == 1 then
print ("pewpewdead")
end
end
hook.Add ("DoPlayerDeath","RegHdsht", RegPlyHeadshots)[/lua]
Just insert your code instead of the "pewpewdead", and it will be run every time a player is being killed with a headshot.
hmm, so you're saying sometimes LastHitGroup() outputs HEAD and sometimes it inputs 1?
Would this work?
[lua]
if victim:LastHitGroup() == 1 or victim:LastHitGroup == HEAD then
do stuff
end
[/lua]
I made it work now, god I should make a video for cyber to show I can get dozens of headshots in a row with an smg, lol if you know what I mean
Use a sniper for testing, except for a SMG
[QUOTE=Andriko1;27353106]hmm, so you're saying sometimes LastHitGroup() outputs HEAD and sometimes it inputs 1?
Would this work?
[lua]
if victim:LastHitGroup() == 1 or victim:LastHitGroup == HEAD then
do stuff
end
[/lua]
I made it work now, god I should make a video for cyber to show I can get dozens of headshots in a row with an smg, lol if you know what I mean[/QUOTE]
I doubt it, the SMG has terrible spread.
Sorry, you need to Log In to post a reply to this thread.