• Need help with for loops and tables
    4 replies, posted
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: [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 ) end ply:Give( WeaponClass ) ply:SetAmmo( ammoamt, ammotype, true ) end sound.Play( 'items/gift_pickup.wav', ply:GetPos() ) end[/CODE] Error: [CODE][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[/CODE] I know [I]what[/I] it's saying, I just don't know [I]why[/I]. 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 [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/GetWeapons]Player:GetWeapons[/url] 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; [lua] 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 ) end end 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 [/lua] 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 [I]object[/I] 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; [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/weapons/Get]weapons.Get[/url] takes a weapon classname and returns a base weapon table, [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/GetWeapon]Player:GetWeapon[/url] acts on a player, takes a classname and returns the Weapon object that they have of that class name, and [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/GetClass]Entity:GetClass[/url] 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.
Sorry, you need to Log In to post a reply to this thread.