Players not receiving weapons.

Hey guys, I have this table and some functions in shared.lua

But whenever I start up the game mode it gives me this error.

[ERROR] gamemodes/ghostbusters/gamemode/shared.lua:17: ‘=’ expected near ‘weaponstable’

  1. unknown - gamemodes/ghostbusters/gamemode/shared.lua:0

You don’t need format before a table, else if needs to be written together…

Make sure ply is set to the player metatable. And I’m not sure if ply:Give( ) works on multiple entries. Never used it that way… If not do this:

[lua]for k, v in pairs( { “weapon_crowbar”, “weapon_fists”, “weapon_slam”, “weapon_mad_flare”, “weapon_mad_c4” } ) do
ply:Give( v )
end[/lua]

Here:
[lua]function ply:GiveGamemodeWeapons( )
if n == 0 then
ply:Give( table.Random( weaponstable ) )
elseif n == 1 then
ply:Give(“weapon_crowbar”, “weapon_fists”, “weapon_slam”, “weapon_mad_flare”, “weapon_mad_c4”)
end

weaponstable = {}
weaponstable[1] = "weapon_mad_357"
weaponstable[2] = "weapon_mad_usp"
weaponstable[3] = "weapon_mad_deagle"
weaponstable[4] = "weapon_mad_57"
weaponstable[5] = "weapon_mad_glock"
weaponstable[6] = "weapon_mad_dual"
weaponstable[7] = "weapon_mad_auto_glock"
weaponstable[8] = "weapon_mad_usp_match"
weaponstable[9] = "weapon_mad_p228"

end[/lua]

This is all great, but I’m a beginner and have always been confused about this

for k, v in pairs( { “weapon_crowbar”, “weapon_fists”, “weapon_slam”, “weapon_mad_flare”, “weapon_mad_c4” } ) do
ply:Give( v )
end

What exactly does for k, v in pairs mean?

EDIT: Also, its giving me this

[ERROR] gamemodes/ghostbusters/gamemode/shared.lua:10: attempt to index global ‘ply’ (a nil value)

  1. unknown - gamemodes/ghostbusters/gamemode/shared.lua:10
  2. include - [C]:-1
    3. unknown - gamemodes/ghostbusters/gamemode/init.lua:4

I’d imagine that means I need to do what you said and set ply to the player meta table, I’m looking into it on the gmod wiki about meta tables but can’t seem to find much thats informative. Could you help me out with this too? I’d hate to be annoying but I cant figure it out.

Its essentially a loop/iteration. One of the primary core functions to any programming language.

k, v can be set as anything, but k is short for key ( All tables have keys/indexes ), v is short for value.

So in the example I gave, we iterate through this table: { “weapon_crowbar”, “weapon_fists”, “weapon_slam”, “weapon_mad_flare”, “weapon_mad_c4” }

If we were to print( k ), it would output:


1
2
3
4
5

If we were to print( v ), it would output:


weapon_crowbar
weapon_fists
weapon_slam
weapon_mad_flare
weapon_mad_c4

You can do:
[lua]for key, value in pairs( { “weapon_crowbar”, “weapon_fists”, “weapon_slam”, “weapon_mad_flare”, “weapon_mad_c4” } ) do
for meh, heh in pairs( { “weapon_crowbar”, “weapon_fists”, “weapon_slam”, “weapon_mad_flare”, “weapon_mad_c4” } ) do
for sup, man in pairs( { “weapon_crowbar”, “weapon_fists”, “weapon_slam”, “weapon_mad_flare”, “weapon_mad_c4” } ) do[/lua]

And they would all do the same thing, you’d reference the second variable though: value, heh, or man in place of v in the example I gave to get the same result.

Syntax is:
FOREACH LOOP: for key, value in pairs( TABLE ) do // This calls the code within for each element in the table.
WHILE LOOP: while ( true ) do // This one loops continuously until you call break;
Counting loop: for i = 1, 50 do // This one starts at 1, and ends at 50 and increments i along the way

I hope this helps.

Thanks man!