• Change Mode when killing NPCS
    15 replies, posted
Hi all I'm kinda noob in this and I would like to know how I can do in lua this: I have for example a weapon and I want that weapon to change his mode when I kill for example 10 npcs whatever the npc is friendly or not. Sorry for my english I hope you guys understand it
[url]http://wiki.garrysmod.com/page/GM/OnNPCKilled[/url]
[QUOTE=Robotboy655;50639494][url]http://wiki.garrysmod.com/page/GM/OnNPCKilled[/url][/QUOTE] an example would be nice to be honest...
Anyone ?
hook.Add("OnNPCKilled", "asdasd", function( a, b, c ) do stuff end )
[QUOTE=Invule;50640491]hook.Add("OnNPCKilled", "asdasd", function( a, b, c ) do stuff end )[/QUOTE] Like this ? [CODE]hook.Add("OnNPCKilled", "asdasd", function( victim ) if victim:GetClass() == "npc_zombie" then self:ToggleSuppressor() end end) [/CODE] Gives an error when I try to use self:ToggleSuppressor() "attempt to call method 'ToggleSuppressor' (a nil value)"
self is nil, take this: hook.Add("OnNPCKilled", "asdasd", function( victim, killer ) if victim:GetClass() == "npc_zombie" then killer:ToggleSuppressor() end end)
[QUOTE=Invule;50640665]self is nil, take this: hook.Add("OnNPCKilled", "asdasd", function( victim, killer ) if victim:GetClass() == "npc_zombie" then killer:ToggleSuppressor() end end)[/QUOTE] same error... dumb question: that code goes where ?
[QUOTE=Xanatos98;50640728]same error... dumb question: that code goes where ?[/QUOTE] server side
[QUOTE=dannyf127;50640757]server side[/QUOTE] Still doing the same... Also it's just for one weapon not for all weapons. So the code needs to be specifically for that weapon
What's self:ToggleSuppressor() Do you know what parameters does the hook spit? Do you know where is this funciton?
[QUOTE=gonzalolog;50640825]What's self:ToggleSuppressor() Do you know what parameters does the hook spit? Do you know where is this funciton?[/QUOTE] self:ToggleSuppressor() is in the shared.lua inside of the weapon base.
Inflictor doesn't necessarily return the weapon, or a weapon on the base. You can do something like this: [code]hook.Add( "OnNPCKilled", "ToggleSuppressor", function( _, attacker, inflictor ) if ( inflictor.ToggleSuppressor ) then inflictor:ToggleSuppressor() else local pWeapon = attacker:IsPlayer() and attacker:GetActiveWeapon() or NULL if ( pWeapon.ToggleSuppressor ) then pWeapon:ToggleSuppressor() end end end )[/code]
[QUOTE=code_gs;50640921]Inflictor doesn't necessarily return the weapon, or a weapon on the base. You can do something like this: [code]hook.Add( "OnNPCKilled", "ToggleSuppressor", function( _, attacker, inflictor ) if ( inflictor.ToggleSuppressor ) then inflictor:ToggleSuppressor() else local pWeapon = attacker:IsPlayer() and attacker:GetActiveWeapon() or NULL if ( pWeapon.ToggleSuppressor ) then pWeapon:ToggleSuppressor() end end end )[/code][/QUOTE] You deserver a medal ! THANK YOU SO MUCH! Edit: Maybe a modification to go togglesuppressor after 10 npcs killed and then a timer of 30 seconds and togglesuppressor again to get back to unsilenced sequences. I barely know how to do things like this to be honest Thank you anyways :D
[lua] hook.Add( "OnNPCKilled", "ToggleSuppressor", function( _, attacker, inflictor ) if(!attacker.HasSuppresor) then attacker.SKills = (attacker.SKills or 0) + 1 end if(attacker.SKills >= 10 && !attacker.HasSuppresor) then attacker.HasSuppresor = true timer.Simple(30,function() if(IsValid(attacker)) then attacker.HasSuppresor = nil attacker.Skills = 0 if(attacker.supFunc) then attacker:supFunc() end end end) if ( inflictor.ToggleSuppressor ) then inflictor:ToggleSuppressor() attacker.supFunc = inflictor.ToggleSuppresor else local pWeapon = attacker:IsPlayer() and attacker:GetActiveWeapon() or NULL if ( pWeapon.ToggleSuppressor ) then pWeapon:ToggleSuppressor() attacker.supFunc = pWeapon.ToggleSuppresor end end end end ) [/lua] not tested, no notifications, since i don't know how your gamemode handles it
[QUOTE=gonzalolog;50640994][lua] hook.Add( "OnNPCKilled", "ToggleSuppressor", function( _, attacker, inflictor ) if(!attacker.HasSuppresor) then attacker.SKills = (attacker.SKills or 0) + 1 end if(attacker.SKills >= 10 && !attacker.HasSuppresor) then attacker.HasSuppresor = true timer.Simple(30,function() if(IsValid(attacker)) then attacker.HasSuppresor = nil attacker.Skills = 0 if(attacker.supFunc) then attacker:supFunc() end end end) if ( inflictor.ToggleSuppressor ) then inflictor:ToggleSuppressor() attacker.supFunc = inflictor.ToggleSuppresor else local pWeapon = attacker:IsPlayer() and attacker:GetActiveWeapon() or NULL if ( pWeapon.ToggleSuppressor ) then pWeapon:ToggleSuppressor() attacker.supFunc = pWeapon.ToggleSuppresor end end end end ) [/lua] not tested, no notifications, since i don't know how your gamemode handles it[/QUOTE] AWESOME !! WORKS HELLA GOOD !!! Edit: When going togglesuppressor reset the counter of 10 NPCs so when the weapon gets out of the Suppressor anims don't activate inmediately
Sorry, you need to Log In to post a reply to this thread.