1. Post #1
    Roamingdream's Avatar
    May 2011
    6 Posts
    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.

  2. Post #2
    amkoc's Avatar
    January 2011
    562 Posts
    This should work:

    drop in autorun and save as whatever.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
    
    Reply With Quote Edit / Delete Reply Mac United States Show Events Disagree Disagree x 1 (list)

  3. Post #3
    Roamingdream's Avatar
    May 2011
    6 Posts
    When I type SaveWeapons it says Unknown Command, LoadWeapons works though.

  4. Post #4
    Awesome Gmod Servers!
    brandonj4's Avatar
    September 2011
    2,065 Posts
    Change:
    concommand.Add( "SaveWeapons", SaveWeapons )

    to:
    concommand.Add( "SaveWeapons", saveWeapons )

  5. Post #5
    Roamingdream's Avatar
    May 2011
    6 Posts
    [@lua\autorun\whatever.lua:19] bad argument #1 to 'pairs' (table expected, got string)

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

  6. Post #6
    Gold Member
    cis.joshb's Avatar
    January 2011
    1,854 Posts
    PData can't be a table.
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Agree Agree x 2 (list)

  7. Post #7
    One of the million useless Gold Members
    Kittai's Avatar
    December 2010
    707 Posts
    As said above, PData stores strings, ints and floats.

    You could use Glon for this.
    Reply With Quote Edit / Delete Reply Windows Vista United States Show Events Agree Agree x 1 (list)

  8. Post #8
    Roamingdream's Avatar
    May 2011
    6 Posts
    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.

  9. Post #9
    Gold Member
    thejjokerr's Avatar
    December 2007
    2,077 Posts
    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);
    Reply With Quote Edit / Delete Reply Windows 7 Netherlands Show Events Winner Winner x 1 (list)

  10. Post #10
    Roamingdream's Avatar
    May 2011
    6 Posts
    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);
    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!

  11. Post #11
    amkoc's Avatar
    January 2011
    562 Posts
    As said above, PData stores strings, ints and floats.

    You could use Glon for this.
    derp, I was going to but I forgot, sorry OP

  12. Post #12
    Roamingdream's Avatar
    May 2011
    6 Posts
    derp, I was going to but I forgot, sorry OP
    Its fine. Thanks.

  13. Post #13
    "The superior man understands what is right; the inferior man understands what will sell"
    Chessnut's Avatar
    August 2011
    3,442 Posts
    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);

    Haven't tested.