New to Lua, Working on a Gamemode idea [need help]

Hey guys, I’m working on a gamemode idea of using the Customizable Weaponry 2.0 addon and making a “MilSim” search and destroy type gamemode with squads against teams of enemies (modified combine elites). At the moment I’m working on testing the CW2.0 mod out and It’s working in my new gamemode with added files, but whenever I try to set teams (each ‘team’ assigns a main weapon), it gives me this error:

[ERROR] gamemodes/concreteplayground/gamemode/init.lua:16: attempt to call method ‘team’ (a nil value)

  1. Call - gamemodes/concreteplayground/gamemode/init.lua:16
  2. unknown - gamemodes/base/gamemode/player.lua:274
    3. Spawn - [C]:-1
    4. unknown - gamemodes/concreteplayground/gamemode/init.lua:35
    5. unknown - lua/includes/modules/concommand.lua:54

This is the code inside the init.lua:

include( "shared.lua" )

function set_team()
local frame = vgui.Create( "DFrame" )
frame:SetPos( 100, ScrH() / 2 ) --Set the window in the middle of the players screen/game window
frame:SetSize( 200, 300 ) --Set the size
frame:SetTitle( "Choose Weapon" ) --Set title
frame:SetVisible( true )
frame:SetDraggable( false )
frame:ShowCloseButton( true )
team_1 = vgui.Create( "DButton", frame )
team_1:SetPos( frame:GetTall() / 30, 30 ) --Place it half way on the tall and 5 units in horizontal
team_1:SetSize( 100, 50 )
team_1:SetText( "Sniper" )
team_1.DoClick = function() --Make the player join team 1

    RunConsoleCommand( "team_1" )

team_2 = vgui.Create( "DButton", frame )
team_2:SetPos( frame:GetTall() / 30, 85 ) --Place it next to our previous one
team_2:SetSize( 100, 50 )
team_2:SetText( "Marksman" )
team_2.DoClick = function() --Make the player join team 2

    RunConsoleCommand( "team_2" )


team_3 = vgui.Create( "DButton", frame )
team_3:SetPos( frame:GetTall() / 30, 140 ) --Place it next to our previous one
team_3:SetSize( 100, 50 )
team_3:SetText( "Assault" )
team_3.DoClick = function() --Make the player join team 3

    RunConsoleCommand( "team_3" )


team_4 = vgui.Create( "DButton", frame )
team_4:SetPos( frame:GetTall() / 30, 195 ) --Place it next to our previous one
team_4:SetSize( 100, 50 )
team_4:SetText( "Specialist" )
team_4.DoClick = function() --Make the player join team 4

    RunConsoleCommand( "team_4" )


concommand.Add( "team_menu", set_team )

Any ideas on what to fix? Because of this error It won’t spawn the player with assigned weapons because of the ‘nil value’ error.

First of all embed your code in


Second of all, that RunConsoleCommand stuff is garbage and is breaking your code. You haven't defined those console commands on the server

Third of all if you did want to do this you should probably make those variables local because that's gonna make some problems.
  1. Thanks, new here didn’t realise, I’ll remember next time.
  2. What would be an substitute? I’m new to writing my own Lua, mostly just basic stuff.
  3. Yeah I saw that on another thread, I’ll get to that.

Thank you!

I suggest you learn lua first by practicing smaller stuff, like easy small addons.

Yeah, I had an idea for this gamemode and I tried setting it out in sandbox and working on some small modifications to it, and I liked it so I started working on the gamemode but ran into some small errors like that.

P.S Which variables do I make local?

On absorbing the whole code I see a bunch of problems happening here.

You should localize the variables that are making buttons. Every time you call vgui.Create you are creating whatever element you defined. That means that every time the console command is run, you will create a new element. That means that you could potentially be making 100 buttons stacking on top of each other, especially because you aren’t removing anything when it’s clicked.

Moreso you should maybe set the frame you are parenting everything to as a global, and check to see if it already exists to prevent creating 1000 copies of the same, then removing the frame when a button is clicked ( and thus removing all the buttons as well )

But that’s not even half the issue. I notice you put this in INIT meaning you want the server to run it. SERVERS cannot create vgui elements for clients, so you’d want this all to be in your CL_INIT file. The concommand.Add’s are okay to put on the INIT, but you will need to fix how this functions as a whole to get the effect you want.

You will need to send a command over to the CLIENT in order to create that vgui stuff. NET commands probably being the best

[editline]23rd November 2015[/editline]

Honestly I respect a man with ambitions but you gotta take the baby steps to get there. Brush up a little on LUA before you tack on a whole gamemode. You’ll need to learn the difference between CLIENT and SERVER as well as their limitations, and get some basic understanding of what’s happening under the hood. Best of luck

Oops! I must have said INIT, I mean’t cl_init.lua, that code is Client, my bad. That’s all client stuff

But thanks so much, I should work on some lua indeed, at the moment I have an alright understanding of it but putting it to use, I’ll definitely need to learn it. My only coding experience lies in C++, Java and Web dev.

Thanks again