• Help: Disarm all players and NPCs
    23 replies, posted
Hi there, I'm trying to use a hook that will disarm all the NPCs and Players (Except you) in the server or on single player. Heres the code to remove your own weapon. I realize this is only for the ar2. I want it to be all the weapons, but I don't know how: hook.Add( "KeyPress", "keypress_use_music", function( ply, key ) if ( key == IN_USE ) then player.GetByID( 1 ):StripWeapon( "weapon_ar2" ) end end ) Thanks!
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/StripWeapons]Player:StripWeapons[/url]
for k, v in pairs(player.GetAll()) do v:StripWeapons()
[QUOTE=dannyf127;50546264]for k, v in pairs(player.GetAll) do v:StripWeapons()[/QUOTE] So I place this in the above hook, and it should work? EDIT: I tried this hook.Add( "KeyPress", "keypress_use_WeaponStrip", function( ply, key ) if ( key == IN_USE ) then for k, v in pairs(player.GetAll) do v:StripWeapons() end end end ) But when I ran it I got an error that said bad argument #1 to 'pairs' (table expected, got function) I'm probably using it wrong. Can you help please?
[QUOTE=tcmaxwell2;50556079]So I place this in the above hook, and it should work? EDIT: I tried this hook.Add( "KeyPress", "keypress_use_WeaponStrip", function( ply, key ) if ( key == IN_USE ) then for k, v in pairs(player.GetAll) do v:StripWeapons() end end end ) But when I ran it I got an error that said bad argument #1 to 'pairs' (table expected, got function) I'm probably using it wrong. Can you help please?[/QUOTE] It was my mistake change: for k,v in pairs(player.GetAll) do to: for k,v in pairs(player.GetAll()) do [editline]20th June 2016[/editline] Also for future reference so you understand what is going on. right now I'm using a loop. The loop, loops over player.GetAll() which has all the players in it. v is what is being returned, so since we're looping over all the players v, will be assigned to every player. Similar to ply:StripWeapons(), in this case we're doing v:StripWeapons(). Keep in mind v, can be any letter as long as you reference the same letter you initially used.
[QUOTE=dannyf127;50556232]It was my mistake change: for k,v in pairs(player.GetAll) do to: for k,v in pairs(player.GetAll()) do [editline]20th June 2016[/editline] Also for future reference so you understand what is going on. right now I'm using a loop. The loop, loops over player.GetAll() which has all the players in it. v is what is being returned, so since we're looping over all the players v, will be assigned to every player. Similar to ply:StripWeapons(), in this case we're doing v:StripWeapons(). Keep in mind v, can be any letter as long as you reference the same letter you initially used.[/QUOTE] From what I'm getting, he wants it to strip weapons from every player, besides the person pressing the key. Which is the code below. (untested code) [lua]hook.Add("KeyPress", "keypress_use_WeaponStrip", function(ply, key) if key == IN_USE then for k, v in pairs(player.GetAll()) do if v == ply:SteamID() then return end v:StripWeapons() end end end)[/lua]
[QUOTE=Matsumoto;50556413]It would be this, from what I'm getting, he wants it to strip weapons from every player, besides the person pressing the key. (untested code) [lua]hook.Add("KeyPress", "keypress_use_WeaponStrip", function(ply, key) if key == IN_USE then for k, v in pairs(player.GetAll()) do if v == ply:SteamID() then return end v:StripWeapons() end end end)[/lua][/QUOTE] No no no, return ends the function and may end the loop even it hasnt been looped fully through, you are comparing player to string also... [CODE] if v == ply then continue end [/CODE]
[QUOTE=SteppuFIN;50556431]No no no, return ends the function and may end the loop even it hasnt been looped fully through, you are comparing player to string also... [CODE] if v == ply then continue end [/CODE][/QUOTE] Was on my phone and not thinking. Thanks for the correction.
So, just to clarify here, what code should I be using in my Addon? [editline]21st June 2016[/editline] Also, will this work for NPCs too? If not, how do I go about that one?
[QUOTE=tcmaxwell2;50560583]So, just to clarify here, what code should I be using in my Addon? [editline]21st June 2016[/editline] Also, will this work for NPCs too? If not, how do I go about that one?[/QUOTE] [CODE] hook.Add("KeyPress", "keypress_use_WeaponStrip", function(ply, key) if key == IN_USE then for k, v in pairs(player.GetAll()) do if v != ply then v:StripWeapons() end end end) [/CODE]
[QUOTE=danker pepers;50560815][CODE] hook.Add("KeyPress", "keypress_use_WeaponStrip", function(ply, key) if key == IN_USE then for k, v in pairs(player.GetAll()) do if v != ply then v:StripWeapons() end end end) [/CODE][/QUOTE] You are missing end for the if statement. [CODE] hook.Add("KeyPress", "keypress_use_WeaponStrip", function(ply, key) if key == IN_USE then for k, v in pairs(player.GetAll()) do if v != ply then v:StripWeapons() end end end end) [/CODE]
Ok, this works for all Players which has been tried and tested so thanks for that, now how do I do the same with all NPCs? Do I use a NPCs table?
Regarding NPCs, untested but this might work: [lua]for k, v in pairs(ents.GetAll()) do if v:IsNPC() then v:GetActiveWeapon():Remove() end end[/lua]
Wait, nevermind. It works ^_^ Thanks [editline]21st June 2016[/editline] [QUOTE]hook.Add("KeyPress", "keypress_use_WeaponStrip", function(ply, key) if key == IN_USE then for k, v in pairs(player.GetAll()) do if v == ply then continue end v:StripWeapons() end for k, v in pairs(ents.GetAll()) do if v:IsNPC() then v:GetActiveWeapon():Remove() end end end end)[/QUOTE] Is that right?
Ok, so sorry to bring this back up, but after a few hours of testing, I realized that its not just isolated to when the weapon is out. It removes weapons every-time you press E. So how do I get it to only work when weapon is out? Thanks!
add this to the top of the hook: [lua]local wep = ply:GetActiveWeapon() if not IsValid(wep) then return end if wep:GetClass() ~= "your_weapons_class_name" then return end[/lua]
')' expected (to close '(' at line 216) near 'end' hook.Add("KeyPress", "keypress_use_WeaponStrip", function(ply, key) local wep = ply:GetActiveWeapon() if not IsValid(wep) then return end if wep:GetClass() ~= "Bloop" then return end if key == IN_USE then for k, v in pairs(player.GetAll()) do if v == ply then continue end v:StripWeapons() end for k, v in pairs(ents.GetAll()) do if v:IsNPC() then v:GetActiveWeapon():Remove() end end else end end end) [editline]24th June 2016[/editline] How do I fix that??
Please use [noparse][lua][/noparse] tags And let us know which line is 216.
You have an extra end at the bottom, you have three, there should only be two.
[Lua]hook.Add("KeyPress", "keypress_use_WeaponStrip", function(ply, key) local wep = ply:GetActiveWeapon() if not IsValid(wep) then return end if wep:GetClass() ~= "weapon_quicksilver_fists_mod_extension" then return end if key == IN_USE then for k, v in pairs(player.GetAll()) do if v == ply then continue end v:StripWeapons() end for k, v in pairs(ents.GetAll()) do if v:IsNPC() then v:GetActiveWeapon():Remove() end end else end end) [\Lua] The hook.add line is 216 [editline]26th June 2016[/editline] [QUOTE=NeatNit;50586778]Please use [noparse][lua][/noparse] tags And let us know which line is 216.[/QUOTE]
[QUOTE=tcmaxwell2;50598898]How do I use Lua Tags.[/QUOTE] [noparse][lua]code goes here[/lua][/noparse] Becomes: [lua]code goes here[/lua] Here's your code with lua tags btw: [lua]hook.Add("KeyPress", "keypress_use_WeaponStrip", function(ply, key) local wep = ply:GetActiveWeapon() if not IsValid(wep) then return end if wep:GetClass() ~= "weapon_quicksilver_fists_mod_extension" then return end if key == IN_USE then for k, v in pairs(player.GetAll()) do if v == ply then continue end v:StripWeapons() end for k, v in pairs(ents.GetAll()) do if v:IsNPC() then v:GetActiveWeapon():Remove() end end else end end)[/lua]
[lua]hook.Add("KeyPress", "keypress_use_WeaponStrip", function(ply, key) local wep = ply:GetActiveWeapon() if not IsValid(wep) then return end if wep:GetClass() ~= "weapon_quicksilver_fists_mod_extension" then return end if key == IN_USE then for k, v in pairs(player.GetAll()) do if v == ply then continue end v:StripWeapons() end for k, v in pairs(ents.GetAll()) do if v:IsNPC() then v:GetActiveWeapon():Remove() end end else end end)[/lua] [editline]26th June 2016[/editline] Removed the end and now i get this error: [lua] [ERROR] addons/quicksilver_mod/lua/weapons/weapon_quicksilver_fists_mod_extension/shared.lua:232: Tried to use a NULL entity! 1. Remove - [C]:-1 2. v - addons/quicksilver_mod/lua/weapons/weapon_quicksilver_fists_mod_extension/shared.lua:232 3. unknown - lua/includes/modules/hook.lua:84[/lua]
Here's a hint: what happens when you strip an NPC that has already been stripped?
[QUOTE=NeatNit;50598966]Here's a hint: what happens when you strip an NPC that has already been stripped?[/QUOTE] Good point. Thanks!
Sorry, you need to Log In to post a reply to this thread.