Need help with for loops and tables

Okay guys, here I go again. I’m trying to look at all of the weapons the player has equipped when he buys this item and strip them of any that share a weapon kind with WeaponClass. Everything works up until I try to strip the weapon, when I get the error shown below.

My code:

ITEM.Name = 'AK47' --Name to be shown in Pointshop
ITEM.Price = 0 --Price of weapon
ITEM.Model = 'models/weapons/w_rif_ak47.mdl' --Model to be shown in Pointshop
ITEM.SingleUse = true
local WeaponClass = 'weapon_ttt_ak47'

function ITEM:OnBuy(ply)
	local wep = weapons.Get(WeaponClass)
	local ammoamt = wep.Primary.DefaultClip
	local ammotype = wep.Primary.Ammo

	for _, k in pairs(ply:GetWeapons()) do
		if k.Kind == wep.Kind then
			print( k ) --Here to tell me if it successfully found the correct weapons (which it did)
			ply:StripWeapon( k )
		ply:Give( WeaponClass )
		ply:SetAmmo( ammoamt, ammotype, true )
	sound.Play( 'items/gift_pickup.wav', ply:GetPos() )


[ERROR] addons/pointshop-master/lua/items/weapons/ak47.lua:13: attempt to index local 'k' (a number value)
    1. OnBuy - addons/pointshop-master/lua/items/weapons/ak47.lua:13
        2. PS_BuyItem - addons/pointshop-master/lua/sv_player_extension.lua:239
            3. addons/pointshop-master/lua/sv_pointshop.lua:4
                4. unknown - lua/includes/modules/net.lua:32

I know what it’s saying, I just don’t know why. Any suggestions?

oh wow I’m dumb.

ply:StripWeapon ( k:GetClass () ) is what you’re looking for I believe

ply:GetWeapons() does return a table, however the objects in this table are not Weapons objects, they are numbers. Hence why on line 13 you get an error telling you that you are treating a number as a table ( K.Kind).

I tested some code in game, and the function

Player:GetWeapons does indeed return a table of Weapon objects. However, you need to convert those into the weapon tables that they represent to get the value ‘wep’ out of them. Here’s the code for that;

for _, k in pairs(ply:GetWeapons()) do
local wepclass = k:GetClass()
if weapons.Get( wepclass ).Kind == wep.Kind then
print( wepclass ) --Here to tell me if it successfully found the correct weapons (which it did)
ply:StripWeapon( wepclass )
ply:Give( WeaponClass )
ply:SetAmmo( ammoamt, ammotype, true ) – move these lines outside the loop, they aren’t dependent on k and don’t need to be run more than once

One thing you should understand (and something that tripped me up quite a few times in the past) is that there are three different ways to represent a weapon; a classname, a Weapon object, and a base weapon table.
A weapon classname is just a string of the weapon’s class name; “weapon_crowbar” or “weapon_357”. It doesn’t contain any extra information; it’s just a string of text.
A weapon object on the other hand, is a representation of the Weapon-type entity that a player carries. It contains information about the weapons primary and secondary ammo clips, it’s current animation, and things specific to each weapon that any player holds.
A base weapon table, on the other hand, is what you define in the weapon’s SWEP file. It contains information nonspecific to any single weapon object, but rather that is shared with every instance of that weapon that exists; its viewmodel, its clip size, its sounds and firing actions, and so on.

There are various functions in Glua that allow you to translate between each of these different representations;

weapons.Get takes a weapon classname and returns a base weapon table,

Player:GetWeapon acts on a player, takes a classname and returns the Weapon object that they have of that class name,

Entity:GetClass can be used on a Weapon object to get the classname.

Thanks so much for your help again Maurdekye, and especially for taking the time to explain all of that to me.