Checking weapons then stripping certain ones?

I’m attempting to check a players weapons by ply:GetWeapons() and I try to check with


	for k, v in pairs(ply:GetWeapons()) do
		if v == "weapon_zm_rifle" then
			ply:StripWeapon(v:GetPrintName())
		end
	end

However, it does not work. I receive a “GetPrintName()” is a nil value.

I can see the weapons are printed as so:


1	=	Weapon [184][weapon_zm_carry]
2	=	Weapon [185][weapon_zm_improvised]
3	=	Weapon [186][weapon_ttt_unarmed]
4	=	Weapon [6][weapon_zm_rifle]


How would I go about checking against the weapons? I assumed checking a string (such as "weapon_zm_rifle) would work but it doesn’t. Kinda clueless as to what to do. I’ve searched around to no avail. Any help will be appreciated. :slight_smile:

Have you looked at DarkRP’s weapon checker? Look at the DarkRP github. It could help you :stuck_out_tongue:

No I haven’t but I’ll give it a look, thanks!

The GetWeapons method returns a table of the weapon entities that the player has. If you want to check their class, you have to use the GetClass method and check that, it is also used as the parameter for the StripWeapon method.
[lua]local strip = {
[“weapon_class”] = true,
[“weapon_class2”] = true,
}

for k, wep in pairs( ply:GetWeapons() ) do

local class = wep:GetClass()
if strip[ class ] then ply:StripWeapon( class ) end

end[/lua]

Ah okay. I’ve been tinkering around and got this on my own:


	for k, v in pairs(ply:GetWeapons()) do
		if v:GetClass() == "weapon_zm_rifle" then
			ply:StripWeapon(v:GetClass())
		end
	end

Now say I wanted to check against a list of weapons (which I can see yours does the same, but I’m trying to figure out other ways so I know in the future) and while I’m not entirely sure if this works this way but I’ve seen this done (may be incorrect ofc)


	local weapons = {
	"weapon_zm_rifle",
	"weapon_zm_mac10"
	}


Which it obviously doesn’t work as I can’t recall exactly what it was. I do know I would have to change from


if v:GetClass() == "weapon_zm_rifle"

to


if v:GetClass() == weapons

The way mine works, the table “strip” has a list of keys, all which point to the value true. The point being that instead of iterating through the table for each weapon on the player and checking if its class matches one in the table, I can index the table using the class. If what is returned is true (ie I’ve put the item in the table), the “if” will run and strip the weapon from the player. If the table is indexed using a class I have not added to the table, nothing (nil) will be returned, which Lua evaluates as false, so the weapon is not stripped.
The reason why this is more efficient is because you don’t have to iterate through another table for every item in the player’s weapon table.
If you wanted to do it without indexing, you could use table.HasValue like so
[lua]for k, wep in pairs( ply:GetWeapons() ) do

local class = wep:GetClass()
if table.HasValue( classes, class ) then ply:StripWeapon( class ) end

end[/lua]
Which is effectively the same as
[lua]for k, wep in pairs( ply:GetWeapons() ) do

local class = wep:GetClass()
for k, otherclass in pairs( classes ) do
	
	if class == otherclass then ply:StripWeapon( class ) end

end

end[/lua]
See the inefficiency?

Ahh I see, thank you!

I’m still learning so I wanted to go ahead and ask since I’m curious and want to learn, but wouldn’t it be possible to have a sort-of global “strip” so that another file that I use in the future can call from that table? If so, is it as easy as removing local, or?