Custom Loadouts

I would like a script that will allow players to select which weapons they start with when they spawn. They can do this via Console. Type “loadout_select” and they can select which weapons to start with.

Thanks in advance.

[lua]
require( “glon” )

function CustomLoadout( ply )

for k, v in pairs( glon.decode( ply:GetNetworkedString( "custom_loadout" ) ) ) do
	ply:Give( v )
end

return true

end
hook.Add( “PlayerLoadout”, “Custom”, Loadout)

function UpdateLoadout( ply, cmd, args )

ply:SetNetworkedString( "custom_loadout", glon.encode( args ) )

end
concommand.Add( “loadout_select”, UpdateLoadout )
[/lua]

Goes in garrysmod/lua/autorun/server, on the server.

So you want to network a potentially large table as a string and send it to every client connected even though they can’t do anything with it? Think before using Networked Variables.

Thanks. But how do I use it? I tried typing the concommand, then type the weapons I wanted, but it didn’t do anything.

Did you respawn after typing the concommand?

Try this:
[lua]
require( “glon” )

CustomLoadouts = { }
function CustomLoadout( ply )

for k, v in ipairs( player.GetAll() ) do
        if v == ply then
            for a, b in pairs( CustomLoadouts[k] ) do
                ply:Give( b )
            end
        end
end

return true

end
hook.Add( “PlayerLoadout”, “Custom”, Loadout)

function UpdateLoadout( ply, cmd, args )

    for k, v in ipairs( player.GetAll() ) do
       if ply == v then
       CustomLoadouts[k] = args
           gamemode.Call( "PlayerLoadout", ply )
       end
    end

end
concommand.Add( “loadout_select”, UpdateLoadout )
[/lua]

Still not working.

-strokes beard-

Any errors?

The script looks ok to me. Did you use a valid weapon name?

None. It just won’t do anything.

Yeah. I type


loadout_select "weapon_crowbar"

and respawn. Nothing. I type “loadout_select” and it switches me back to physgun.

Try: loadout_select weapon_pistol

It’s “weapon_crowbar”

Whoops, I forgot the _

[lua]ply.wepslist = {}[/lua] in GM:PlayerInitialSpawn

[lua]

    ply:StripWeapons()
    ply:StripAmmo()

    if ply.wepslist[1] then
	
	for k,v in pairs(ply.wepslist) do
		
		ply:Give(v)
	end
else		
	ply:Give( "weapon_physcannon" ) --Give the player the Gravity Gun
	ply:Give( "weapon_physgun" )
	ply:Give( "gmod_tool" )
	ply:Give( "gmod_camera" )
	ply:Give( "weapon_crowbar" )

end[/lua] in GM:PlayerLoadout

and [lua]function choose_weps( ply, cmd, args )

if ply:IsValid() then
	
	for k,v in pairs(args) do
		
		ply.wepslist[k] = v
	end
end

end

concommand.Add(“choose_weps”, choose_weps)[/lua] somewhere in the GM’s init.lua.

This eliminates the need for costly NWStrings. Just type “choose_weps weapon_pistol weapon_crossbow weapon_crowbar” etc in console, should work fine. Not to break a long FP tradition, but I actually tested this, and it does work. :stuck_out_tongue:

How would I install that?

may i ask WHERE DO YOU FIND THE GM:PlayerLoadout IN???
CAUSE I AM A NEWB AT THIS

it’d be useful to specify a gamemode

If the gamemode doesn’t have the function for that. Make one, other than that, just search with notepad++.

i did. it now is not working. i put that in the init.lua? cause that might be why it wont work