Permanent Weapons

Hi there! im wanting to make a permanent weapon system where i can type in console permwep “STEAMID” “WEAPON”
im quite new to lua and confused on how to target the player and how to save the table unique to them

this is what i came up with so far which is not good at all


function AddWeapons( ply )
ply.UsersWeps={} 
ply.UsersWeps[1]="First Entry" 

table.insert( ply.UserWeps, VARIABLEFORGIVENWEAPON )

ply:SetPData( "weapons", ply.UserWeps )

local ply.GetMyWeapons = ply:GetPData( "weapons" )
	ply:Give( "ply.GetMyWeapons")
	
end

please can someone help me?



local playerMeta = FindMetaTable("Player")

function playerMeta:GivePermaWeapons( wep )
    local weps = self:GetPData("weapons")

    table.insert(weps, wep)

    self:SetPData("weapons", weps)
    self:Give(wep)
    
end

ply:GivePermaWeapon("weapon_smg")


Then you would use the

GM/PlayerSpawn hook to give them the weapon again when they spawn.

wow thank you so much! ive needed this since shrek shutdown coderhire!

[editline]6th May 2015[/editline]

so if im right ply:GivePermaWeapon adds that weapon to their permanent weapons?

[editline]6th May 2015[/editline]

calling me dumb is cool but not very helpful, so would the console command be something like this



concommand.Add( "permawep_add", function( victim, weapon )
		victim:GivePermaWeapons(weapon)
end)


concommand.Add

http://puu.sh/hDKi9.png

sorry but im still confused by this on what cmd, args and argStr is

The first key of the table of arguments would be the weapon (args) which is the third argument of the callback function.

Use these to see whats in a variable:

Global.print

Global.PrintTable

so would it be like this?



concommand.Add( "permawep_add", function(args, args2 )
		print(args, args2)
end)


it wouldnt just print it aswell

http://puu.sh/hDM3s.png

!=

http://puu.sh/hDM61.jpg

Do the maths

like this?



concommand.Add( "permawep_add", function(args, argStr )
		print(args, argStr)
end)


but it only prints


 Player [1]N00bister      permawep_add

No.



concommand.Add("permawep_add", function(ply, cmd, args)
    print(ply, args[1])
end)


thanks but now i still have the problem with the code Starfox gave me

My code:



concommand.Add("permawep_add", function(ply, cmd, args)
    ply:GivePermaWeapon(args[2])
end)


it brings up this error




[ERROR] gamemodes/gt/gamemode/permweapons.lua:14: attempt to call method 'GivePermaWeapon' (a nil value)
  1. unknown - gamemodes/gt/gamemode/permweapons.lua:14
   2. unknown - lua/includes/modules/concommand.lua:54


[editline]6th May 2015[/editline]

and by the way the command im using is
permawep_add N00bister weapon_smg

[editline]6th May 2015[/editline]

same with this



concommand.Add("permawep_add", function(ply, cmd, args)
    args[1]:GivePermaWeapon(args[2])
end)


He forgot an s, so it should be ply:GivePermaWeapons. Also, you need to convert the table to a JSON string using

util.TableToJSON because tables do not save to PData; all it does is convert the table to a string using

sql.SQLstr and saves it in a TEXT field. Also, you need to have a default value otherwise you will throw a Lua error if there is no PData entry. Try this:



local playerMeta = FindMetaTable('Player')

function playerMeta:GivePermaWeapons( wep )
    local weps = util.JSONToTable(self:GetPData("weapons", '')) or {}

    table.insert(weps, wep)

    self:SetPData("weapons", util.TableToJSON(weps))
    self:Give(wep)
    
end

ply:GivePermaWeapons("weapon_smg")


this still isnt working, its saying that Give is a nil value?

Post the full error message and the entire code you are using.

my code



local playerMeta = FindMetaTable('Player')

function playerMeta:GivePermaWeapons( wep )
    local weps = util.JSONToTable(self:GetPData("weapons", '')) or {}

    table.insert(weps, wep)

    self:SetPData("weapons", util.TableToJSON(weps))
    self:Give(wep)
    
end

function GivePermWeapon(ply, cmd, args)
	ply:GivePermaWeapons(args[2])
end
concommand.Add( "add_permweap", GivePermWeapon )


my error




[ERROR] gamemodes/gt/gamemode/permweapons.lua:26: attempt to call method 'Give' (a nil value)
  1. GivePermaWeapons - gamemodes/gt/gamemode/permweapons.lua:26
   2. unknown - gamemodes/gt/gamemode/permweapons.lua:32
    3. unknown - lua/includes/modules/concommand.lua:54


[editline]7th May 2015[/editline]

also i know i spelt wep wrong

What color the error is? Is it by any chance orange?

in console its yellow and on my screen (top left) its white

You are trying to give the weapon to a player from the CLIENT. What you should be doing is giving the player the weapon from the SERVER. To ensure this, make sure to:

  1. Include the file permweapons.lua from init.lua
  2. Wrap all of that code in “if ( SERVER ) then /your code here/ end”
  3. Restart the game & server and try again.

ok thanks il try those

[editline]7th May 2015[/editline]

that works but if i type in console add_permwep weapon_smg it says this



Attempted to create unknown entity type weapon_smg!
NULL Ent in GiveNamedItem!


my code



function GivePermWeapon(ply, cmd, args)
	ply:GivePermaWeapons(args[1])
end
concommand.Add( "add_permwep", GivePermWeapon )


It’s weapon_smg1, not weapon_smg