• Headshot checker
    21 replies, posted
Can someone tell me how to make a script that checks for headshots? Would I have to put in the gamemode or in a swep's code?
server sided gamemode code: [lua]function headshot(ply, hitgroup) if (hitgroup == HITGROUP_HEAD) then --do shit here end end hook.Add("PlayerDeath", "headshots", headshot)[/lua] [editline]7th December 2010[/editline] Or use the ScalePlayerDamage hook, so it happens [b]every[/b] headshot.
[lua] hook.Add ("ScalePlayerDamage", "lalaladoda", function ( ply, hitgroup, dmginfo ) if hitgroup == HITGROUP_HEAD then print ("Boom") end end) [/lua] You would need to put it in lua/autorun/server, or addons/abc123/lua/autorun/server
What's the difference?
[QUOTE=c-unit;26548402]What's the difference?[/QUOTE] The hook, and I print "Boom".
Hmm, so... would this give the player that got the headshot 50 points? [lua] ply.point = 0 hook.Add ("ScalePlayerDamage", "lalaladoda", function ( ply, hitgroup, dmginfo ) if hitgroup == HITGROUP_HEAD then ply.point = ply.point + 50 end end) [/lua]
[QUOTE=Andriko1;26553160]Hmm, so... would this give the player that got the headshot 50 points? [lua] ply.point = 0 hook.Add ("ScalePlayerDamage", "lalaladoda", function ( ply, hitgroup, dmginfo ) if hitgroup == HITGROUP_HEAD then ply.point = ply.point + 50 end end) [/lua][/QUOTE] That would give the player that got a bullet stuck in his skull, 50 points. [lua] ply.point = 0 hook.Add ("ScalePlayerDamage", "lalaladoda", function ( ply, hitgroup, dmginfo ) local ply = dmginfo:GetAttacker() if hitgroup == HITGROUP_HEAD then ply.point = ply.point + 50 end end)[/lua]
rofl ok thanks a lot, btw what would be the difference between ply.point and ply:point
[QUOTE=Andriko1;26553351]rofl ok thanks a lot, btw what would be the difference between ply.point and ply:point[/QUOTE] Uh, I don't really understand it myself, but basically its like adding that variable to the table "ply", ply:point would have to be defined in the players metatable or something. here's something thejjokker made in a request thread of mine, it checks if a player has god mode enabled, and if they do it returns true. [lua]local pMeta = FindMetaTable("Player"); local oldGodEnable = pMeta.GodEnable; local oldGodDisable = pMeta.GodDisable; function pMeta:GodEnable() self.GodMode = true; oldGodEnable(self); end function pMeta:GodDisable() self.GodMode = false; oldGodDisable(self); end function pMeta:IsGodded() return self.GodMode; end[/lua]
hook.Add ("ScalePlayerDamage", "lalaladoda", function ( ply, hitgroup, dmginfo ) local ply = dmginfo:GetAttacker() if hitgroup == HITGROUP_HEAD then ply.point = ply.point + 50 print( ply:Nick() .. " got 50 points for a headshot!") end end) so would this print if I got a headshot? "Andriko got 50 points for a headshot!"
[QUOTE=Andriko1;26553543]hook.Add ("ScalePlayerDamage", "lalaladoda", function ( ply, hitgroup, dmginfo ) local ply = dmginfo:GetAttacker() if hitgroup == HITGROUP_HEAD then ply.point = ply.point + 50 print( ply:Nick() .. " got 50 points for a headshot!") end end) so would this print if I got a headshot? "Andriko got 50 points for a headshot!"[/QUOTE] Yes. But that would print it to the console PrintMessage ( HUD_PRINTTALK, ply:Nick().." got 50 points for a headshot!")
It works :) although when you get a headshot with a shotgun, it prints it multiple times as well as gives multiple points times 50... and crossbow doesnt give headshots, as well as rpg. Is there any way to fix that?
[QUOTE=Andriko1;26553954]It works :) although when you get a headshot with a shotgun, it prints it multiple times as well as gives multiple points times 50... and crossbow doesnt give headshots, as well as rpg. Is there any way to fix that?[/QUOTE] I don't know about the crossbow or rpgs, I'm guessing because they use a physics object, for instance a crossbow tip and a missile. [lua] ply.point = 0 local delay = 1 hook.Add ("ScalePlayerDamage", "lalaladoda", function ( victim, hitgroup, dmginfo ) local ply = dmginfo:GetAttacker() if hitgroup == HITGROUP_HEAD and delay > 0 then delay = 0 ply.point = ply.point + 50 PrintMessage ( HUD_PRINTTALK, ply:Nick().." got 50 points for a headshot!") timer.Simple(0.1, delay = 1) end end) [/lua] try that, change 0.1 of timer simple higher if necessary.
[QUOTE=Andriko1;26553351]rofl ok thanks a lot, btw what would be the difference between ply.point and ply:point[/QUOTE] ply.point refers to the class "ply", used in object oriented programming. ply:point would run on ply.
uhh, what? also zeph why did yo uchange ply to victim in the paramaters for the hook
[QUOTE=Andriko1;26555305]uhh, what? also zeph why did yo uchange ply to victim in the paramaters for the hook[/QUOTE] because if you ever wanted to do something related to the guy who got shot, e.g remove points, using ply would remove points for the attacker. [editline]8th December 2010[/editline] By the way, I suggest in the function you add this [lua] print (hitgroup) [/lua] and kill someone with a crossbow/rpg, look at your console (or if its a dedicated server, look at the command box) and see what the hitgroup was, then you can do something like this: [lua] if hitgroup == HITGROUP_XYZ and ply:GetActiveWeapon() == "weapon_crossbow" then etcetcetc elseif hitgroup == HITGROUP_ZYX and ply:GetActiveWeapon() == "weapon_rpg" then etcetcetc elseif hitgroup == HITGROUP_HEAD and delay > 0 then etcetcetc end [/lua]
[QUOTE=thejjokerr;26548556]Yours doesn't work. Dont make up supplied arguments.[/QUOTE] Works just fine for me :3
Doing ply:point() would run the metamethod point() on the player that's specified in the ply variable. Doing ply:point() it's the same as doing ply.point(ply)
[QUOTE=c-unit;26560568]Works just fine for me :3[/QUOTE] [B][URL="http://wiki.garrysmod.com/?title=Gamemode.PlayerDeath"]Gamemode.PlayerDeath [IMG]http://wiki.garrysmod.com/favicon.ico[/IMG][/URL][/B] GM:PlayerDeath( [URL="http://wiki.garrysmod.com/?title=Player"][IMG]http://wiki.garrysmod.com/images/0/0c/Player.png[/IMG][/URL] Victim, [URL="http://wiki.garrysmod.com/?title=Entity"][IMG]http://wiki.garrysmod.com/images/c/c0/Entity.png[/IMG][/URL] Inflictor, [URL="http://wiki.garrysmod.com/?title=Player"][IMG]http://wiki.garrysmod.com/images/0/0c/Player.png[/IMG][/URL] Killer ) Your 'argument' is invalid.
the timer added to the code broke the script entirely :( [editline]8th December 2010[/editline] NVM a fail on my part
Sorry, you need to Log In to post a reply to this thread.