Persistent Weapon Inventory Question

I have a variable v whos datatype is Weapon, but i need it to be the string name of said weapon for use with ply:Give(<string>). How can i do this? Its pretty much the final cog in my afformentioned Persistent Weapon Inventory System.

v:GetClass() I think. Returns a string of the entity (in this case weapon)'s classname.

mkay, ill try it. I just thought GetClass() only worked for entities


Nope, ply:Give( v:GetClass() ) gives me this error:

[@gamemodes\zombie_war_wip\gamemode\init.lua:44] Tried to use a NULL entity!(Hook: PlayerLoadout)

The weapon itself doesn’t exist.


Also, a weapon is an entity.

Could do with seeing some more code.

function GM:PlayerLoadout( ply )

	ply:GiveAmmo( 256,	"Pistol", 		true )
	ply:GiveAmmo( 256,	"SMG1", 		true )
	ply:GiveAmmo( 5,	"grenade", 		true )
	ply:GiveAmmo( 64,	"Buckshot", 	true )
	ply:GiveAmmo( 32,	"357", 			true )
	ply:GiveAmmo( 32,	"XBowBolt", 	true )
	ply:GiveAmmo( 6,	"AR2AltFire",   true )
	ply:GiveAmmo( 100,	"AR2", 			true )

if(ply:HasWeapon("weapon_physgun")) then
local filename = "zw_data/" .. tostring( ply:UniqueID() ) .. "loadout.txt"

if ( file.Exists(filename) ) then
	local read = file.Read( filename )
	local loadout = glon.decode( read )
	for k, v in pairs( loadout ) do
		ply:Give( v:GetClass() )

return true


function SaveWeapons( ply )

local filename = "zw_data/" .. tostring( ply:UniqueID() ) .. "loadout.txt"
local data = glon.encode( ply:GetWeapons() )
file.Write( filename, data )

hook.Add( “DoPlayerDeath”, “SaveWeapons”, SaveWeapons )
hook.Add( “PlayerDisconnected”, “SaveWeapons”, SaveWeapons )

Thats the code im using. Im sure its not the greatest since im new to Lua, but there ya go.

Also, noone steal my code, its mine and ive worked very hard to create it.

Come one people, it cant be that hard to figure out, and its very important i get this tiny iota of code.

You were already told the answer.

You can’t encode weapons, just encode their class names instead.

sooooooo… instead of saving ply:GetWeapons() in the file and get names out of it later, I should get the names out of them, save THEM into a table, and then decode them on spawn?

function GetWepsForSave§
local tab = {}
for k,v in pairs(p:GetWeapons()) do
table.insert(tab, v:GetClass())
return glon.encode(tab)

yeah, thats what i thought to do.


YAY! It works fine now. One last question: Lets say i wanted to make it so when the player types !dropweapon it removes the players curretn weapon and creates it in front of him so either he or someone else can pick it up?

Either use… DropWeapon() or create a custom entity that reads the sweps data.

i meant more like, how do you get the code to run when the player says !dropweapon? is there a hook for the chat box updating or something?


Nevermind, i found the hook i need and got it all to work fine. Even made it so you cant drop physgun, gravgun, toolgun and camera. :smiley: