Tables and Buttons

[lua]function ShopMenu()
local guns = {}
guns[1] = “models/weapons/w_mach_m249para.mdl”
guns[2] = “models/weapons/w_rif_galil.mdl”
guns[3] = “models/weapons/w_rif_ak47.mdl”
guns[4] = “models/weapons/w_rif_m4a1.mdl”
guns[5] = “models/weapons/w_rif_famas.mdl”
guns[6] = “models/weapons/w_snip_awp.mdl”
guns[7] = “models/weapons/w_snip_scout.mdl”
guns[8] = “models/weapons/w_rif_aug.mdl”
guns[9] = “models/weapons/w_rif_sg552.mdl”
guns[10] = “models/weapons/w_snip_g3sg1.mdl”
guns[11] = “models/weapons/w_snip_sg550.mdl”
guns[12] = “models/weapons/w_smg_ump45.mdl”
guns[13] = “models/weapons/w_smg_p90.mdl”
guns[14] = “models/weapons/w_smg_tmp.mdl”
guns[15] = “models/weapons/w_smg_mp5.mdl”
guns[16] = “models/weapons/w_smg_mac10.mdl”
guns[17] = “models/weapons/w_shot_xm1014.mdl”
guns[18] = “models/weapons/w_shot_m3super90.mdl”
guns[19] = “models/weapons/w_eq_fraggrenade.mdl”
guns[20] = “models/weapons/w_eq_flashbang_thrown.mdl”
guns[21] = “models/weapons/w_eq_smokegrenade.mdl”
guns[22] = “models/weapons/w_pist_elite_dropped.mdl”
guns[23] = “models/weapons/w_pist_deagle.mdl”
guns[24] = “models/weapons/w_pist_usp.mdl”
guns[25] = “models/weapons/w_pist_p228.mdl”
guns[26] = “models/weapons/w_pist_glock18.mdl”
guns[27] = “models/weapons/w_pist_fiveseven.mdl”
guns[28] = “models/weapons/w_knife_t.mdl”

local description = {}
description[1] = "M249-Para" 
description[2] = "Galil" 
description[3] = "Ak-47" 
description[4] = "M4A1" 
description[5] = "Famas" 
description[6] = "AWP" 
description[7] = "Scout" 
description[8] = "Augs" 
description[9] = "SG552" 
description[10] = "G3SG1" 
description[11] = "SG550" 
description[12] = "UMP-45" 
description[13] = "P90" 
description[14] = "TMP" 
description[15] = "MP5" 
description[16] = "MAC-10" 
description[17] = "M4Shotgun" 
description[18] = "M3Shotgun" 
description[19] = "FragNade" 
description[20] = "FlashBang" 
description[21] = "SmokeGrenade" 
description[22] = "Dual-Elites" 
description[23] = "Desert-Eagle" 
description[24] = "USP" 
description[25] = "P228" 
description[26] = "Glock18" 
description[27] = "Five-Seven" 
description[28] = "Knife" 

local DermaFrame = vgui.Create("DFrame")
local IconList = vgui.Create( "DPanelList", DermaFrame ) 

	DermaFrame:SetSize(770, 580)

	IconList:EnableVerticalScrollbar( false ) 
	IconList:EnableHorizontal( true ) 
	IconList:SetPadding( 5 ) 
	IconList:SetSize(370, 230)
	local icon1 = vgui.Create( "SpawnIcon", IconList ) 
	icon1:SetModel( guns[1] )
	IconList:AddItem( icon1 )
	icon1.DoClick = function( icon1 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon2 = vgui.Create( "SpawnIcon", IconList ) 
	icon2:SetModel( guns[2] )
	IconList:AddItem( icon2 )
	icon2.DoClick = function( icon2 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon3 = vgui.Create( "SpawnIcon", IconList ) 
	icon3:SetModel( guns[3] )
	IconList:AddItem( icon3 )
	icon3.DoClick = function( icon3 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon4 = vgui.Create( "SpawnIcon", IconList ) 
	icon4:SetModel( guns[4] )
	IconList:AddItem( icon4 )
	icon4.DoClick = function( icon4 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon5 = vgui.Create( "SpawnIcon", IconList ) 
	icon5:SetModel( guns[5] )
	IconList:AddItem( icon5 )
	icon5.DoClick = function( icon5 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon6 = vgui.Create( "SpawnIcon", IconList ) 
	icon6:SetModel( guns[6] )
	IconList:AddItem( icon6 )
	icon6.DoClick = function( icon6 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon7 = vgui.Create( "SpawnIcon", IconList ) 
	icon7:SetModel( guns[7] )
	IconList:AddItem( icon7 )
	icon7.DoClick = function( icon7 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon8 = vgui.Create( "SpawnIcon", IconList ) 
	icon8:SetModel( guns[8] )
	IconList:AddItem( icon8 )
	icon8.DoClick = function( icon8 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon9 = vgui.Create( "SpawnIcon", IconList ) 
	icon9:SetModel( guns[9] )
	IconList:AddItem( icon9 )
	icon9.DoClick = function( icon9 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon10 = vgui.Create( "SpawnIcon", IconList ) 
	icon10:SetModel( guns[10] )
	IconList:AddItem( icon10 )
	icon10.DoClick = function( icon10 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon11 = vgui.Create( "SpawnIcon", IconList ) 
	icon11:SetModel( guns[11] )
	IconList:AddItem( icon11 )
	icon11.DoClick = function( icon11 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon12 = vgui.Create( "SpawnIcon", IconList ) 
	icon12:SetModel( guns[12] )
	IconList:AddItem( icon12 )
	icon12.DoClick = function( icon12 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon13 = vgui.Create( "SpawnIcon", IconList ) 
	icon13:SetModel( guns[13] )
	IconList:AddItem( icon13 )
	icon13.DoClick = function( icon13 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon14 = vgui.Create( "SpawnIcon", IconList ) 
	icon14:SetModel( guns[14] )
	IconList:AddItem( icon14 )
	icon14.DoClick = function( icon14 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon15 = vgui.Create( "SpawnIcon", IconList ) 
	icon15:SetModel( guns[15] )
	IconList:AddItem( icon15 )
	icon15.DoClick = function( icon15 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon16 = vgui.Create( "SpawnIcon", IconList ) 
	icon16:SetModel( guns[16] )
	IconList:AddItem( icon16 )
	icon16.DoClick = function( icon16 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon17 = vgui.Create( "SpawnIcon", IconList ) 
	icon17:SetModel( guns[17] )
	IconList:AddItem( icon17 )
	icon17.DoClick = function( icon17 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon18 = vgui.Create( "SpawnIcon", IconList ) 
	icon18:SetModel( guns[18] )
	IconList:AddItem( icon18 )
	icon18.DoClick = function( icon18 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon19 = vgui.Create( "SpawnIcon", IconList ) 
	icon19:SetModel( guns[19] )
	IconList:AddItem( icon19 )
	icon19.DoClick = function( icon19 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon20 = vgui.Create( "SpawnIcon", IconList ) 
	icon20:SetModel( guns[20] )
	IconList:AddItem( icon20 )
	icon20.DoClick = function( icon20 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon21 = vgui.Create( "SpawnIcon", IconList ) 
	icon21:SetModel( guns[21] )
	IconList:AddItem( icon21 )
	icon21.DoClick = function( icon21 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon22 = vgui.Create( "SpawnIcon", IconList ) 
	icon22:SetModel( guns[22] )
	IconList:AddItem( icon22 )
	icon22.DoClick = function( icon22 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon23 = vgui.Create( "SpawnIcon", IconList ) 
	icon23:SetModel( guns[23] )
	IconList:AddItem( icon23 )
	icon23.DoClick = function( icon23 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon24 = vgui.Create( "SpawnIcon", IconList ) 
	icon24:SetModel( guns[24] )
	IconList:AddItem( icon24 )
	icon24.DoClick = function( icon24 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon25 = vgui.Create( "SpawnIcon", IconList ) 
	icon25:SetModel( guns[25] )
	IconList:AddItem( icon25 )
	icon25.DoClick = function( icon25 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon26 = vgui.Create( "SpawnIcon", IconList ) 
	icon26:SetModel( guns[26] )
	IconList:AddItem( icon26 )
	icon26.DoClick = function( icon26 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon27 = vgui.Create( "SpawnIcon", IconList ) 
	icon27:SetModel( guns[27] )
	IconList:AddItem( icon27 )
	icon27.DoClick = function( icon27 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 
	local icon28 = vgui.Create( "SpawnIcon", IconList ) 
	icon28:SetModel( guns[28] )
	IconList:AddItem( icon28 )
	icon28.DoClick = function( icon28 ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gs_buy_m249") end 

concommand.Add(“open”, ShopMenu)[/lua]

Could someone show how to make this into one function instead of having 28 buttons please? O.o

local Weapons = {};

Weapons[“models/weapons/w_mach_m249para.mdl”] = { “Description Here”, “gs_buy_command” };

for model,info in pairs( Weapons ) do

	local icon = vgui.Create( "SpawnIcon", IconList );

    icon:SetModel( model );
    icon:SetToolTip( info[1] )
    icon.DoClick = function( icon)
		surface.PlaySound( "ui/buttonclickrelease.wav" );
		RunConsoleCommand( info[2] );
	IconList:AddItem( icon )


if you want a function to add them then

function AddWeapon( model, description, buycommand )
Weapons[model] = { description, buycommand };

I remember when I was at your skill level, you’ll catch up soon enough. Look on the Lua pil about for loops

Thank you for putting this code could you please explain what:
icon:SetToolTip( info[1] )
RunConsoleCommand( info[2] );
info isn’t a table?
And why do you put a colon after the lines?

I put a ; after lines because in C++ you have to.

Okay so basically, in this loop i’m doing

for model, info in pairs

model and info aren’t tables, you could do

for k,v in pairs or

for f,g in pairs

ANYTHING, it does not matter. basically the first thing( model ), is the KEY in the table you’re looping through. and the second is the VALUE of that key in the table.

for example

test = {};

test[“KEY”] = “VALUE”

for key,value in pairs( test ) do
print( key … " " … value );

and the result will be:


make sense?

He made a table called weapons, and he made the index of the “weapons” table the model of the gun, and for the arguments of each gun, a description and what happens when you click the button, basicly to make it most “automated” possible.

The “info” are the arguments of each gun inside the “Weapons” table.
info[1] being the first argument of the gun inside the table. The description of the gun to appear when you hover the button, in his example: “Description Here”

And info[2] being the second argument of the gun inside the table. The command to be run when clicked. in his example: “gs_buy_command”

So for the M249 you’d probably have something like:

Weapons[“models/weapons/w_mach_m249para.mdl”] = { “A Light Machine Gun, fires 5.56×45 mm NATO rounds.”, “gs_buy_m249” }

For the colon, I’m not sure, wanted to know that too.

Didn’t see his post, lol

yours described a few things mine didn’t so don’t remove it. anyway if you have any other questions you can pm me i’ll help you no problem

It sort of makes sense but I think I understand the for loop now (model,info) now.
I’ll try to explain this you correct me because I don’t know C++ at all and that colon thing makes no sense.
[lua]for model,info in pairs( Weapons )[/lua]
For model means its getting the model of the weapon and info is the part the model equals to of the table Weapons.
The model part means the first part in the table right? When it calls info 1 of the table its really calling the first value its equal to:
[lua]{ “Description Here”, “gs_buy_command” }[/lua]
And the second part of the info retreiving would be gs_buy_command. I think I have it but you might have it in different words than I.

So you basically use the colons because you are used to c++?
They don’t change anything at the code then?

in C++ when you put a ; it means to end the line.

it has no effect at all in lua( as far as i know, but i still think it has a newline meaning ), it’s just habbit and it will not effect the code what so ever.

[editline]2nd January 2012[/editline]

Yep, you got it right. It was hard to understand what you meant but I’m confident you understand.

I know that in c++ it means to end the line, just a habit then.

Yeah thanks, before I tried looking inside DarkRP for the loop but I couldn’t understand what he did but its all good now, Thanks for the support and Happy new year! :smiley:

When was that, last week?

[editline]1st January 2012[/editline]

Oh, I mistaked you for tazy4tazy, my apologies.

If you guys are really having troubles with tables and hate taking time to type it all out row by row heres an amazing and super helpful video I found!

How is this dumb it saved me 1 hour of typing…

[lua] local Weapons = {}
Weapons[“weapon_real_cs_m249”] = { “M249-Para” , “gs_buy_m249” }
Weapons[“weapon_real_cs_galil”] = { “Galil” , “gs_buy_galil” }
Weapons[“weapon_real_cs_ak47”] = { “Ak-47” , “gs_buy_ak47” }
Weapons[“weapon_real_cs_m4a1”] = { “M4A1” , “gs_buy_command” }
Weapons[“weapon_real_cs_famas”] = { “Famas” , “gs_buy_command” }
Weapons[“weapon_real_cs_awp”] = { “AWP” , “gs_buy_command” }
Weapons[“weapon_real_cs_scout”] = { “Scout” , “gs_buy_command” }
Weapons[“weapon_real_cs_aug”] = { “Augs” , “gs_buy_command” }
Weapons[“weapon_real_cs_sg552”] = { “SG552” , “gs_buy_command” }
Weapons[“weapon_real_cs_g3sg1”] = { “G3SG1” , “gs_buy_command” }
Weapons[“weapon_real_cs_sg550”] = { “SG550” , “gs_buy_command” }
Weapons[“weapon_real_cs_ump_45”] = { “UMP-45” , “gs_buy_command” }
Weapons[“weapon_real_cs_p90”] = { “P90” , “gs_buy_command” }
Weapons[“weapon_real_cs_tmp”] = { “TMP” , “gs_buy_command” }
Weapons[“weapon_real_cs_mp5a5”] = { “MP5” , “gs_buy_command” }
Weapons[“weapon_real_cs_mac10”] = { “MAC-10” , “gs_buy_command” }
Weapons[“weapon_real_cs_xm1014”] = { “M4Shotgun” , “gs_buy_command” }
Weapons[“weapon_real_cs_pumpshotgun”] = { “M3Shotgun” , “gs_buy_command” }
Weapons[“weapon_real_cs_grenade”] = { “FragNade” , “gs_buy_command” }
Weapons[“weapon_real_cs_flash”] = { “FlashBang” , “gs_buy_command” }
Weapons[“weapon_real_cs_smoke”] = { “SmokeGrenade” , “gs_buy_command” }
Weapons[“weapon_real_cs_elites”] = { “Dual-Elites” , “gs_buy_command” }
Weapons[“weapon_real_cs_desert_eagle”] = { “Desert-Eagle” , “gs_buy_command” }
Weapons[“weapon_real_cs_usp”] = { “USP” , “gs_buy_command” }
Weapons[“weapon_real_cs_p228”] = { “P228” , “gs_buy_command” }
Weapons[“weapon_real_cs_glock18”] = { “Glock18” , “gs_buy_command” }
Weapons[“weapon_real_cs_five-seven” ] = { “Five-Seven” , “gs_buy_command” }
Weapons[“weapon_real_cs_knife”] = { “Knife” , “gs_buy_command” }

function BuyWeapon(ply, ent)
if not ply:GetMoney(500) then
Notify(ply, 1, 4, string.format(“You cannot afford a gun!”)) return “” else
local weapon = ents.Create(“weapon_real_cs_ak47”)
local trace = ply:GetEyeTraceNoCursor()
weapon:SetPos( trace.HitPos )
concommand.Add(“gs_buy_ak47”, BuyWeapon) [/lua]

I dont completely understand your part because i’m doing it differently :stuck_out_tongue:
[lua]function AddWeapon( model, description, buycommand )
Weapons[model] = { description, buycommand };

I need my part to be code as that for the same reason not haveing 28 functions and since I dont know how to AddCSLua or include .lua files since it doesnt make any sense to me. I need a function with all the console commands in one function if possible.

uh what?

Okay stop saying “28 functions” a function is declared as function.

you don’t make 28 functions you make 1 function and then use it 28 times.

after you declare the AddWeapon function you can do

AddWeapon( “models/error.mdl”, “description”, “buycmd” );

your BuyWeapon function makes no sense AT ALL. so idk about that.

Also that video will not help with anything lol

is your first language english? If not how old are you?

Include and AddCSLuaFile are extremely self explanatory if you can’t figure them out then you should stop where you’re at.


– serverside and shared files must be included. If it is shared it must also be included from a clientside file.
include( “file.lua” );

– Clientside files. These must also be included on a clientside file.
AddCSLuaFile( “file.lua” );

[editline]2nd January 2012[/editline]

– Okay, this is the proper way you should be doing this.

S_Weapons = {};

function AddWeapon( strswepclass, strmodel, strdescription, intprice)
if ( !S_Weapons ) then return end;

S_Weapons[ strswepclass ] = { model = strmodel, description = strdescription, price = intprice };


function _R.Player:BuyWeapon( class )
if ( !S_Weapons ) then return end;
if ( !S_Weapons[class] ) then return end;

local wInd = S_Weapons[class];

if ( self:GetMoney() < wInd.price ) then return end;

local swep = ents.Create( class );
swep:SetPos( self:GetPos() );

self:AddMoney( wInd.price*-1);


[lua]S_Weapons = {};

function AddWeapon( strswepclass, strmodel, strdescription, intprice)
if ( !S_Weapons ) then return end;

S_Weapons[ strswepclass ] = { model = strmodel, description = strdescription, price = intprice };


function _R.Player:BuyWeapon( class )
if ( !S_Weapons ) then return end;
if ( !S_Weapons[class] ) then return end;

local wInd = S_Weapons[class];
if ( self:GetMoney() < wInd.price ) then return end;
local swep = ents.Create( class );
swep:SetPos( self:GetPos() );
self:AddMoney( wInd.price*-1);


This made no sense at all and yes, english is my first language and I make it sound like that because lua is my first coding language and I learn better by listening then by visualizing.

I made two functions. One to add a new weapon. Another to attempt to buy a weapon. It will only work if the weapon has been made with AddWeapon.

[lua]S_Weapons = {};
–I understand this is the table right.
–With AddWeapon strswepclass means class strmodel is model, so on so on.
– Do I make the table like this?
S_Weapons[“weapon_class”] = {“models/bla.mdl”, “fuzzy”, “1000”}

function AddWeapon( strswepclass, strmodel, strdescription, intprice)
if ( !S_Weapons ) then return end;
–This part is calling the table.
S_Weapons[ strswepclass ] = { model = strmodel, description = strdescription, price = intprice };
–This part is making variables for the S_Weapons table.
–I dont know what _R is but ill look it up. Allows you to add a function to a specific metatable. Just got it.
–This makes the buyweapon function.
function _R.Player:BuyWeapon( class ) – I DO NOT UNDER STAND THIS ( class ) ???
if ( !S_Weapons ) then return end; – If its not calling the S_Weapons table then end it.
if ( !S_Weapons[class] ) then return end; --If the S_Weapons has no class then end it.

local wInd = S_Weapons[class]; -- Wind is a variable for S_Weapons Class part.
if ( self:GetMoney() < wInd.price ) then return end; --If the player whos buying has less money than price of it then end.
local swep = ents.Create( class ); -- Create the class of the swep im trying to get. As shown here I DO NOT UNDERSTAND Where its geting (Class) from.........
swep:SetPos( self:GetPos() ); --get the position of the player to auto grab it
swep:Spawn(); -- spawn it
self:AddMoney( wInd.price*-1); -- add negative money to the player whos buying it.


S_Weapons[ strswepclass ] This part is confusing because later on you use class… I know I said its a variable earlier but you use local wInd = S_Weapons[class]. Wouldnt it be
S_Weapons[ strswepclass ]?

How Would I add a console command for a whole table of commands I still don’t understand.
[lua]function AddWeapon( model, description, buycommand )
Weapons[model] = { description, buycommand };
If you make that you’re still not doing the concommand.Add(“gs_buy”)
Would it be concommand.Add(“gs_buy”, buycommand) ?

You don’t do

S_Weapons["weapon_class"] = {"models/bla.mdl", "fuzzy", "1000"}

at all. You just do

AddWeapon( “weapon_class”, “models/bla.mdl”, “fuzzy”, 1000 );

which does it all for you.

line 15’s ‘class’ is just a argument. whatever you pass to it through that function is equivalent to class.

for example

function something(class,something,else,name,does,not,matter)
print( class … something … else … name … does … not … matter )

and then do

something("here", "you", "are", "passing", "arguments", "to", "thefunction");

which would print “hereyouarepassingargumentstothefunction”