concommand.Add Doesn't seem to work properly. Error in code?

I’m attempting to add a console command that will allow people to assign a team to themselves but I seem to be having issues with my code. The command gives the generic “Unknown command” message when I try to run it in game or at the server console. I’m sure I have just borked something up but can’t seem to figure it out.

Here is the error I’m seeing on server startup


[ERROR] gamemodes/sandbox_war/gamemode/player.lua:34: attempt to call global 'SetGamemodeTeam' (a nil value)
  1. unknown - gamemodes/sandbox_war/gamemode/player.lua:34
   2. include - [C]:-1
    3. unknown - gamemodes/sandbox_war/gamemode/init.lua:9


[ERROR] gamemodes/sandbox_war/gamemode/player.lua:34: attempt to call global 'SetGamemodeTeam' (a nil value)
  1. unknown - gamemodes/sandbox_war/gamemode/player.lua:34
   2. include - [C]:-1
    3. unknown - gamemodes/sandbox_war/gamemode/init.lua:9


[ERROR] gamemodes/sandbox_war/gamemode/player.lua:34: attempt to call global 'SetGamemodeTeam' (a nil value)
  1. unknown - gamemodes/sandbox_war/gamemode/player.lua:34
   2. include - [C]:-1
    3. unknown - gamemodes/sandbox_war/gamemode/init.lua:13


[ERROR] gamemodes/sandbox_war/gamemode/player.lua:34: attempt to call global 'SetGamemodeTeam' (a nil value)
  1. unknown - gamemodes/sandbox_war/gamemode/player.lua:34
   2. include - [C]:-1
    3. unknown - gamemodes/sandbox_war/gamemode/init.lua:13

Here is my code

players.lua


local ply = FindMetaTable("Player")

local  teams = {}

--teams[0] = {name = "Blue", color = Vector( .2, .2, 1.0 ), weapons = {"fas2_glock20", "fas2_ak47"} }
--teams[1] = {name = "Red", color = Vector( 1.0, .2, .2 ), weapons = {"fas2_glock20", "fas2_ak47"} }

teams[0] = {name = "Blue", color = Vector( .2, .2, 1.0 ), weapons = {"weapon_crowbar"} }
teams[1] = {name = "Red", color = Vector( 1.0, .2, .2 ), weapons = {"weapon_pistol"} }


function ply:SetGamemodeTeam( n )
        if not teams[n] then return end

        self:SetTeam( n )

        self:SetPlayerColor( teams[n].color )

        self:GiveGamemodeWeapons()

        return true
end

function ply:GiveGamemodeWeapons()
        local n = self:Team()
        self:StripWeapons()

        for k, wep in pairs(teams[n].weapons) do
                self:Give(wep)
        end

end

concommand.Add("tselect1", SetGamemodeTeam( 0 ))
concommand.Add("tselect2", SetGamemodeTeam( 1 ))

init.lua


AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
AddCSLuaFile("database/cl_database.lua")
AddCSLuaFile("database/items.lua")
--AddCSLuaFile( "cl_commands.lua" )
AddCSLuaFile ( "player.lua" )

include( "shared.lua" )
include( "player.lua" )
include( "database/database.lua")
include( "database/items.lua")
--include( "cl_commands.lua" )
include ( "player.lua" )

function GM:PlayerConnect (name, ip)
        print("Player: " .. name .. " has joined the game.")
end

function GM:PlayerInitialSpawn ( ply )
        print("Player: " .. ply:Nick() .. " has spawned.")

        ply:SetGamemodeTeam()
end

function GM:PlayerSpawn ( ply )
        ply:SetModel("models/player/group01/male_07.mdl")
        ply:GiveGamemodeWeapons()
end

function GM:PlayerAuthed ( ply, steamID, uniqueID )
        print("Player: " .. ply:Nick() .. " is now authenticated.")
        ply:databaseCheck()
end

function GM:PlayerDisconnected( ply )
        ply:databaseDisconnect()
end


cl_init.lua


include( "shared.lua" )
include( "database/cl_database.lua" )
include( "database/items.lua")
--include( "cl_commands.lua" )
include ( "player.lua" )

shared.lua


GM.Name = "Sandbox War Test"
GM.Author = "elricsfate"
GM.Email = "elricsfate@gmail.com"
GM.Website = "http://typexclan.com"

team.SetUp( 0, "Blue", Color(0, 0, 255) )
team.SetUp( 1, "Red", Color(255, 0, 0) )

function GM:Initialize()
        self.BaseClass.Initialize( self )
end

I’m sure I’ve made some sort of blazingly apparent error. If someone would give me a hand then it would be greatly appreciated.

SetGamemodeTeam() is an undefined function Player:SetGamemodeTeam() is valid though

concommand.Add is working exactly as it should. You’re using it wrong however.

concommand.Add(“tselect1”, SetGamemodeTeam( 0 ))

This is actually calling the function when the script is initalized, wrap it up like so:

concommand.Add(“tselect1”, function() SetGamemodeTeam( 0 ) end)

Thanks for the help so far guys but things still don’t seem to be working properly. Here is my current error and the code I modified


concommand.Add("tselect1", function() ply:SetGamemodeTeam( 0 ) end)
concommand.Add("tselect2", function() ply:SetGamemodeTeam( 1 ) end)

Here is the error I receive.


[ERROR] gamemodes/sandbox_war/gamemode/player.lua:15: attempt to call method 'SetTeam' (a nil value)
  1. SetGamemodeTeam - gamemodes/sandbox_war/gamemode/player.lua:15
   2. unknown - gamemodes/sandbox_war/gamemode/player.lua:34
    3. unknown - lua/includes/modules/concommand.lua:69

The player is then kicked from the server.

Further insight is appreciated.

You didn’t define ply. [lua]concommand.Add(cmd, ply, args)[/lua] should give you an idea what I mean.

I’m going to be honest, I’ve tried everything and things still aren’t working. Do you mean something like this?


concommand.Add("tselect1", function( cmd ply args ) ply:SetGamemodeTeam( 0 ) end)
concommand.Add("tselect2", function( cmd ply args ) ply:SetGamemodeTeam( 1 ) end)

That gives me the same error I recently posted. I’m sure I’m misunderstanding something or doing something wrong but I’m just not getting it :confused:

player, command, then arguments.

Not command, player, arguments.

You aren’t putting commas the in the function’s parameters.

ply, cmd, args

In that order specifically. I’m not going to look at the rest of the code. Just pointing out that one thing.

The concommand should be on the server, not in shared or client.

Thanks for the info. I attempted to write things out like so


concommand.Add("tselect1", function( cmd, ply, args) ply:SetGamemodeTeam( 0 ) end)
concommand.Add("tselect2", function( cmd, ply, args) ply:SetGamemodeTeam( 1 ) end)

I’m still getting unknown command in console though. That is both at the server prompt and client side.

EDIT: removed the things that send to shared and client. Still experiencing the same issue as before. Unknown command.

EDIT2: Correction on that last part. Accidently started server in wrong game mode. Here is the error I’m seeing now.


Player: Roctopus has spawned.

[ERROR] gamemodes/sandbox_war/gamemode/player.lua:35: bad key to string index (number expected, got string)
  1. error - [C]:-1
   2. __index - lua/includes/extensions/string.lua:262
    3. unknown - gamemodes/sandbox_war/gamemode/player.lua:35
     4. unknown - lua/includes/modules/concommand.lua:69


[ERROR] gamemodes/sandbox_war/gamemode/player.lua:35: bad key to string index (number expected, got string)
  1. error - [C]:-1
   2. __index - lua/includes/extensions/string.lua:262
    3. unknown - gamemodes/sandbox_war/gamemode/player.lua:35
     4. unknown - lua/includes/modules/concommand.lua:69

Roctopus suicided!

[ERROR] gamemodes/sandbox_war/gamemode/player.lua:34: bad key to string index (number expected, got string)
  1. error - [C]:-1
   2. __index - lua/includes/extensions/string.lua:262
    3. unknown - gamemodes/sandbox_war/gamemode/player.lua:34
     4. unknown - lua/includes/modules/concommand.lua:69


[ERROR] gamemodes/sandbox_war/gamemode/player.lua:34: bad key to string index (number expected, got string)
  1. error - [C]:-1
   2. __index - lua/includes/extensions/string.lua:262
    3. unknown - gamemodes/sandbox_war/gamemode/player.lua:34
     4. unknown - lua/includes/modules/concommand.lua:69

Roctopus suicided!

[lua]
concommand.Add(“tselect1”, function( ply, cmd, args) ply:SetGamemodeTeam( 0 ) end)
concommand.Add(“tselect2”, function( ply, cmd, args) ply:SetGamemodeTeam( 1 ) end)
[/lua]
Been forever since I’ve used concommands. Put the arguments in the right order that they are passed.