[Help] Inventory/Weaponsave

Intro: hey, my name is Zombie Extinguisher and i am trying to fix this addon --> http://forum.facepunch.com/showthread.php?t=1011962

this is where i have come so far, but i am kinda stuck, i could use all info and tips

Glon is also not the problem

Adapted Code:


function ZmodLoadout( ply )

		//Give the ammunition. This can be taken out if you want the player to spawn with weps but no ammo.
		ply:RemoveAllAmmo()
		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 )
		
		//Sets default weapons. These can be atken out if you want the player to start with nothing.
		
		ply:Give("weapon_physgun")
		ply:Give("weapon_physcannon")
		ply:Give("gmod_camera")
		ply:Give("gmod_tool")
		
		//Loads weapons from a file. The folders and filename can be changed, but not the UniqueID part.
	
	if (file.Exists( "wep_data/" .. tostring( ply:UniqueID() ) .. "loadout.txt" )) then
		local Data = glon.decode(file.Read( "wep_data/" .. ply:UniqueID() .. "loadout.txt" ));

		//////////-- local read = file.Read( "wep_data/" .. ply:UniqueID() .. "loadout.txt" ) [these were the original lines]
		/////////-- local loadout = file.Read( Data ) [these were the original lines]

		for k, v in pairs( Data ) do ////--[  for k, v in pairs( loadout) ] -- this was the original, but didn't work aswel 
			ply:Give( v )
		end 
	end
	
	if(ply:HasWeapon("weapon_physgun")) then
	ply:SelectWeapon("weapon_physgun")
	end
	
	return true

end
hook.Add("PlayerLoadout", "ZmodLoadout", ZmodLoadout)

function SaveWeapons( ply )
	local filename = "wep_data/" .. tostring( ply:UniqueID() ) .. "loadout.txt"
	local wepclasstable = {}
		for d, f in pairs( ply:GetWeapons() ) do
			table.insert( wepclasstable, f:GetClass() )
		end 
	local data = glon.encode( wepclasstable )
	file.Write( filename, data )
end
hook.Add( "DoPlayerDeath", "SaveWeapons", SaveWeapons )
hook.Add( "PlayerDisconnected", "SaveWeapons", SaveWeapons )

The safeweapons function seems to work fine since the loadout appears correctly in wep_data file.

when i respawn i get this error:



[ERROR] addons/server_custom_scripts/lua/autorun/saveweapons.lua:23: bad argument #2 to 'Exists' (string expected, got no value)
  1. Exists - [C]:-1
   2. v - addons/server_custom_scripts/lua/autorun/saveweapons.lua:23
    3. Call - lua/includes/modules/hook.lua:82
     4. PlayerSpawn - gamemodes/base/gamemode/player.lua:251
      5. unknown - gamemodes/sandbox/gamemode/init.lua:41
       6. Spawn - [C]:-1
        7. unknown - gamemodes/base/gamemode/player.lua:103



file.Exists needs a second argument. See http://wiki.garrysmod.com/page/file/Exists

[editline]31st January 2014[/editline]

Same with file.Read.

[editline]31st January 2014[/editline]

You are probably looking for the “DATA” path since you are writing to this file too.

[lua]file.Exists( “wep_data/” … tostring( ply:UniqueID() ) … “loadout.txt”, “DATA” )[/lua]

this gives me a new error, any idea on how to fix that ?

adapted code:


function ZmodLoadout( ply )

		//Give the ammunition. This can be taken out if you want the player to spawn with weps but no ammo.
		ply:RemoveAllAmmo()
		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 )
		
		//Sets default weapons. These can be atken out if you want the player to start with nothing.
		
		ply:Give("weapon_physgun")
		ply:Give("weapon_physcannon")
		ply:Give("gmod_camera")
		ply:Give("gmod_tool")
		
		//Loads weapons from a file. The folders and filename can be changed, but not the UniqueID part.
	
	if (file.Exists( "wep_data/" .. tostring( ply:UniqueID() ) .. "loadout.txt", "DATA" )) then
		local Data = glon.decode(file.Read( "wep_data/" .. ply:UniqueID() .. "loadout.txt", "DATA" ));
		-- local read = file.Read( "wep_data/" .. ply:UniqueID() .. "loadout.txt" ) [these were the original lines]
		-- local loadout = file.Read( Data ) [these were the original lines]
		for k, v in pairs( Data ) do --[  for k, v in pairs( loadout) ] -- this was the original, but didn't work aswel 
			ply:Give( v )
		end 
	end
	
	if(ply:HasWeapon("weapon_physgun")) then
	ply:SelectWeapon("weapon_physgun")
	end
	
	return true

end
hook.Add("PlayerLoadout", "ZmodLoadout", ZmodLoadout)

function SaveWeapons( ply )
	local filename = "wep_data/" .. tostring( ply:UniqueID() ) .. "loadout.txt"
	local wepclasstable = {}
		for d, f in pairs( ply:GetWeapons() ) do
			table.insert( wepclasstable, f:GetClass() )
		end 
	local data = glon.encode( wepclasstable )
	file.Write( filename, data )
end
hook.Add( "DoPlayerDeath", "SaveWeapons", SaveWeapons )
hook.Add( "PlayerDisconnected", "SaveWeapons", SaveWeapons )


the new error



[ERROR] addons/inventoryaddon/lua/ia_init.lua:112: bad argument #1 to 'pairs' (table expected, got nil)
  1. pairs - [C]:-1
   2. unknown - addons/inventoryaddon/lua/ia_init.lua:112



How are we supposed to know know which line is 112? Also, why do you save weapons when player dies?

Well, i do that so i don’t have to rejoin every time i changed something and want to test it;

line 112 is


function ZmodLoadout( ply )

		//Give the ammunition. This can be taken out if you want the player to spawn with weps but no ammo.
		ply:RemoveAllAmmo()
		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 )
		
		//Sets default weapons. These can be atken out if you want the player to start with nothing.
		
		ply:Give("weapon_physgun")
		ply:Give("weapon_physcannon")
		ply:Give("gmod_camera")
		ply:Give("gmod_tool")
		
		//Loads weapons from a file. The folders and filename can be changed, but not the UniqueID part.
	
	if (file.Exists( "wep_data/" .. tostring( ply:UniqueID() ) .. "loadout.txt", "DATA" )) then
		local Data = glon.decode(file.Read( "wep_data/" .. ply:UniqueID() .. "loadout.txt", "DATA" ));
		-- local read = file.Read( "wep_data/" .. ply:UniqueID() .. "loadout.txt" ) [these were the original lines]
		-- local loadout = file.Read( Data ) [these were the original lines]
		for k, v in pairs( Data ) do <<<<<<<<<<<<<<---------------------- Line 112
			ply:Give( v )
		end 
	end
	
	if(ply:HasWeapon("weapon_physgun")) then
	ply:SelectWeapon("weapon_physgun")
	end
	
	return true

end
hook.Add("PlayerLoadout", "ZmodLoadout", ZmodLoadout)

function SaveWeapons( ply )
	local filename = "wep_data/" .. tostring( ply:UniqueID() ) .. "loadout.txt"
	local wepclasstable = {}
		for d, f in pairs( ply:GetWeapons() ) do
			table.insert( wepclasstable, f:GetClass() )
		end 
	local data = glon.encode( wepclasstable )
	file.Write( filename, data )
end
hook.Add( "DoPlayerDeath", "SaveWeapons", SaveWeapons )
hook.Add( "PlayerDisconnected", "SaveWeapons", SaveWeapons )

glon.decode(file.Read( “wep_data/” … ply:UniqueID() … “loadout.txt”, “DATA” ))

returns nil, do you have glon? try deleting the file you try to load.

Why are you using GLON anyway?

Use JSON.

@Robotboy: thanks man, that did the trick !

@Mors Quaedam: i am using Glon since the inventory that i use, uses it too

It is highly advised for you to switch to JSON.

@Robotboy: whats so bad about glon ?

It’s outdated, not supported by anyone, you need to ship it with your mod, unlike JSON.

[editline]1st February 2014[/editline]

+JSON files are more human-readable.

So glon.encode becomes util.TableToJSON
and glon.decode becomes util.JSONToTable ?

Yes. Make you don’t load old files encoded with glon.

thanks man !

the fixed code for newbies :wink:


function ZmodLoadout( ply )

		//Give the ammunition. This can be taken out if you want the player to spawn with weps but no ammo.
		ply:RemoveAllAmmo()
		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 )
		
		//Sets default weapons. These can be atken out if you want the player to start with nothing.
		
		ply:Give("weapon_physgun")
		ply:Give("weapon_physcannon")
		ply:Give("gmod_camera")
		ply:Give("gmod_tool")
		
		//Loads weapons from a file. The folders and filename can be changed, but not the UniqueID part.
	
	if (file.Exists( "wep_data/" .. tostring( ply:UniqueID() ) .. "loadout.txt", "DATA" )) then
		local Data = util.JSONToTable(file.Read( "wep_data/" .. ply:UniqueID() .. "loadout.txt", "DATA" ));
		for k, v in pairs( Data ) do
			ply:Give( v )
		end 
	end
	
	if(ply:HasWeapon("weapon_physgun")) then
	ply:SelectWeapon("weapon_physgun")
	end
	
	return true

end
hook.Add("PlayerLoadout", "ZmodLoadout", ZmodLoadout)

function SaveWeapons( ply )
	local filename = "wep_data/" .. tostring( ply:UniqueID() ) .. "loadout.txt"
	local wepclasstable = {}
		for d, f in pairs( ply:GetWeapons() ) do
			table.insert( wepclasstable, f:GetClass() )
		end 
	local data = util.TableToJSON( wepclasstable )
	file.Write( filename, data )
end
hook.Add( "DoPlayerDeath", "SaveWeapons", SaveWeapons )
hook.Add( "PlayerDisconnected", "SaveWeapons", SaveWeapons )