Weapon Saving script

New here to this site so if this has been posted I never bothered searching.

Anyway I was looking for a script that would let you enter a command such as ‘SaveWeapons’ and It would save the loadout you have.

I had a script like this before that a friend made me (not saying who~) but it was lost to a system reformat that I did and forgot to backup files…

So if anyone could help it would be very appreciated. Thanks

Edit If not the first one then could someone teach me how to create custom loadouts (and maybe editable ingame.) that you could choose to become when you respawn.

This should work:

drop in autorun and save as whatever.lua
[lua]
if SERVER then
local function saveWeapons( ply, cmd, args )
if !ValidEntity(ply) then return end
local weaponTable = {}

	for k, v in pairs(ply:GetWeapons()) do
		weaponTable[k] = v:GetClass() 
	end
	ply:SetPData("SpawnWeapons", weaponTable)
end
 
concommand.Add( "SaveWeapons", SaveWeapons )


local function loadWeapons( ply, cmd, args )
	if !ValidEntity(ply) then return end
	ply:StripWeapons()
	local weaponTable = ply:GetPData("SpawnWeapons", {})
	for k, v in pairs(weaponTable) do
		ply:Give(weaponTable[k]) 
	end
end
concommand.Add( "LoadWeapons", loadWeapons )

end
[/lua]

When I type SaveWeapons it says Unknown Command, LoadWeapons works though.

Change:
concommand.Add( “SaveWeapons”, SaveWeapons )

to:
concommand.Add( “SaveWeapons”, saveWeapons )

[@lua\autorun\whatever.lua:19] bad argument #1 to ‘pairs’ (table expected, got string)

I get this when I type LoadWeapons into the Console. :confused:

PData can’t be a table.

As said above, PData stores strings, ints and floats.

You could use Glon for this.

So could you help me fix this or tell me how I might be able to fix this, because I know nothing about lua. I am looking for someone who could teach me though.

[lua]concommand.Add(“SaveWeapons”, function(ply, cmd, args)
local weps = {};

for _, wep in pairs(ply:GetWeapons())do
	table.insert(weps, wep:GetClass());
end;

ply:SetPData("loadout", glon.encode(weps));

end);

hook.Add(“PlayerLoadout”, “RestoreSavedPlayerLoadout”, function(ply)
local saved = ply:GetPData(“loadout”, false);

if(saved)then
	for _, wep in pairs(glon.decode(saved))do
		ply:Give(wep);
	end;
	
	return true;
end;

end);[/lua]

Thanks. It works, but would there be any way so it spawns with ammo, because the weapons I have use the default Hl2 ammo. If not the script was enough. thanks!

derp, I was going to but I forgot, sorry OP

Its fine. Thanks.

[lua]concommand.Add(“SaveWeapons”, function(ply, cmd, args)
local weps = {};

for _, wep in pairs(ply:GetWeapons())do
	local ammoType = wep:GetPrimaryAmmoType();
	local ammo = ply:GetAmmoCount(ammoType);

	weps[#weps + 1] = {wep:GetClass(), ammo, ammoType};
end;

ply:SetPData( "loadout", glon.encode(weps) );

end);

hook.Add(“PlayerLoadout”, “RestoreSavedPlayerLoadout”, function(ply)
local saved = ply:GetPData(“loadout”, false);

if (saved) then
	for _, wep in pairs( glon.decode(saved) )do
		ply:Give( wep[1] );
		ply:SetAmmo( wep[2], wep[3] );
	end;
	
	return true;
end;

end);[/lua]

Haven’t tested.