• How to print in console when a player fires a gun?
    15 replies, posted
So I have a darkrp server and I want to end the trouble of not having logs of who shot who. It already logs who killed who but not who fired a gun to inflict damage and not kill someone. I know there is SWEP:PrimaryAttack but that would require extracting the weapons. Which would cause me to either create a server content addon or make the users download individual files for each gun. I looked around and found OnTakeDamage or something close to that and other things like that but it seems the functions I find I would need to create a for k v in pairs every second to print who took damage from what, and that would lag the server. Unless extracting the guns and just adding resource.AddWorkshop would work? Any help would be appreciated.
You can either override PrimaryAttack in an OnEntityCreated hook, or if the weapon uses FireBullets, make an EntityFireBullets hook.
So I see nothing with fire bullets in the code so guessing thats not an option, would it be [CODE] hook.Add( "OnEntityCreated", "PrimaryAttack", function( ply ) function SWEP:PrimaryAttack() print("Weapon fired by "..ply:nick) end end ) [/CODE] Im guessing its like that but I dont know if I have to copy the entire primary attack code because you said it overrides it. ?
You have to call the original PrimaryAttack function, and check if the entity is a weapon and is scripted. SWEP does not exist in that hook.
[code] hook.Add("OnEntityCreated", "PrimaryAttack", function(ent) local attack = ent.PrimaryAttack if not attack then return end ent.PrimaryAttack = function(self) attack(self) print("Weapon "..self:GetClass().." fired by "..self:GetOwner()) end end) [/code]
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/EntityFireBullets]GM:EntityFireBullets[/url]
I got this far [CODE] hook.Add("OnEntityCreated", "PrimaryAttack",function(ent) if ent:IsWeapon() == true then SWEP = ent print("is weapon") function ent:PrimaryAttack() attack(self) print("fired") end end end) [/CODE] Now I am stuck again I tried SWEP:PrimaryAttack and ent:PrimaryAttack none of them seem to work. Im probably missing something.
You need to cache SWEP.PrimaryAttack before you override it and call it in the overidden function.
So make the function before the hook then run the function in the hook?
Check mijyuoon's post.
[CODE] hook.Add("OnEntityCreated", "PrimaryAttack",function(ent) if ent:IsWeapon() == true then local attack = ent.PrimaryAttack ent.PrimaryAttack = function(self) attack(self) print("fired") end end end) [/CODE] Like this? But this doesnt print fired?
[QUOTE=Bkamahl;52547479][CODE] hook.Add("OnEntityCreated", "PrimaryAttack",function(ent) if ent:IsWeapon() == true then local attack = ent.PrimaryAttack ent.PrimaryAttack = function(self) attack(self) print("fired") end end end) [/CODE] Like this? But this doesnt print fired?[/QUOTE] This should work for most Lua weapons. For the default HL2 weapons you'd need to make a [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/EntityFireBullets]GM:EntityFireBullets[/url] hook.
It is the CSS on M9K base weapons. The only reason I dont make my own weapons is because I dont know how to make it penetrate, if I cant solve this ima go research on that. But I would like to find out how to solve this anyway. EDIT: I also forgot to say this is darkrp and yes I did put that file in the autorun lua folder of gmod I put a print in the hook to see if it actually ran
If you can't even do this I highly doubt you'd be able to make your own weapon base.
[QUOTE=txike;52547489]If you can't even do this I highly doubt you'd be able to make your own weapon base.[/QUOTE] Hey man, I can still try.
Welp looks like firebullets thingy worked.
Sorry, you need to Log In to post a reply to this thread.