Weapon selection Menu

Hey there,

I’m trying to make an simple weapon selection menu to get back into Glua.
But there are a few things that bother me that are:

  1. I got the selection and weapon give thing working by doing this:
    Cl_init
    [lua]DButton.DoClick = function() local primary = PrimaryBox:GetSelectedItems()[1]:GetValue()
    local second = SecondBox:GetSelectedItems()[1]:GetValue()
    local third = ThirdBox:GetSelectedItems()[1]:GetValue()

if primary == “Glock” then
RunConsoleCommand(“weapon_glock”)
end

if second == “M4a1” then
RunConsoleCommand(“weapon_m4a1”)
end

if third == “HE Grenade” then
RunConsoleCommand(“weapon_hegrenade”)
end

selectorFrame:Close()
end

end[/lua]
and init.lua:
[lua]function glock( ply )

ply:Give(“weapon_real_cs_glock18”)
ply:GiveAmmo(100,“pistol”)
end
concommand.Add(“weapon_glock”, glock)

// Second Box

function m4a1( ply )

ply:Give(“weapon_real_cs_m4a1”)
ply:GiveAmmo(250,“smg1”)
end
concommand.Add(“weapon_m4a1”, m4a1)

// Third Box
function hegrenade( ply )

ply:Give(“weapon_real_cs_grenade”)
end
concommand.Add(“weapon_hegrenade”, hegrenade)

[/lua]

It works but isnt there a simple way of doing this? and/or somehow you can’t pick it from Console?

  1. When I die I lose all the weapons and I want back my previous weapons not by selecting from the weapon menu because it will take to long. Any Glua things I should look at for that?

Thanks,

[lua]
concommand.Add( “weapon_choose”, function( ply, cmd, args )

if not args[ 1 ] then return end

ply:Give( args[ 1 ] )

end )

– example: RunConsoleCommand( “weapon_choose”, “weapon_real_cs_grenade” )
[/lua]

You should probably add in a check to see if they’re choosing an allowed weapon or not though. Otherwise they could just shove “weapon_choose weapon_rpg” in their console.

As for your second point, if you only want it to save across deaths, then you can just keep it on the players table, for example:

[lua]
if not ply.Weapons then ply.Weapons = {} end
table.insert( ply.Weapons, args[ 1 ] )
[/lua]

Then when the player respawns just loop through ply.Weapons and give them each weapon in there.

If you want it to save across disconnects, however, I suggest you look into using **[Player.SetPData

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Player.SetPData)** or you can use raw SQL. If you wanted you could also save it to a text file.

That works thanks!
But the only thing is if nothing is selected it gives out an error and does nothing and i tried everything to fix it, help?

What’s the error?

[gamemodes\skeleton\gamemode\cl_init.lua:166] attempt to index field ‘?’ (a nil value)
Thats line : [lua]primary = PrimaryBox:GetSelectedItems()[1]:GetValue()[/lua]

You need to add in a check to see if something is selected:

[lua]if PrimaryBox:GetSelectedItems() then
– code
end[/lua]

[lua]DButton.DoClick = function()

if PrimaryBox:GetSelectedItems() != NULL then
if PrimaryBox:GetSelectedItems()[1]:GetValue() == “Glock” then
RunConsoleCommand( “weapon_choose”, “weapon_real_cs_glock18” )
end
end

if SecondBox:GetSelectedItems() != NULL then
if SecondBox:GetSelectedItems()[1]:GetValue() == “M4a1” then
RunConsoleCommand( “weapon_choose”, “weapon_real_cs_m4a1” )

end

end

if ThirdBox:GetSelectedItems() != NULL then
if ThirdBox:GetSelectedItems()[1]:GetValue() == “HE Grenade” then
RunConsoleCommand( “weapon_choose”, “weapon_real_cs_grenade” )

end

end

selectorFrame:Close()
end[/lua]

This works Gives me weapons and stuff but, It still gives the error and doesn’t close menu then

NULL is not the same as nil, it would have to be

[lua]if PrimaryBox:GetSelectedItems() != nil then
– or
if PrimaryBox:GetSelectedItems() then – you don’t really need the != nil bit
[/lua]

I got it working thanks! :smiley:
I did,
[lua]
if ThirdBox:GetSelectedItems() and ThirdBox:GetSelectedItems()[1] then
if ThirdBox:GetSelectedItems()[1]:GetValue() == “HE Grenade” then
RunConsoleCommand( “weapon_choose”, “weapon_real_cs_grenade” )
end
end
[/lua]
And now it works!
Thank you very much :smiley: