Lua Help Needed - Setting Player Teams

EDIT - I’ve been able to fix this with help from here. Thanks guys.

Hello. I am making a simple game mode and have encountered a problem. The client cannot execute a concommand in shared.lua . I have looked for solutions in several places.
When I try to execute team_1 or team_2 in console it says this:

] team_1
Test\gamemode\shared.lua:14: attempt to call method 'SetTeam' (a nil value)
] team_2
Test\gamemode\shared.lua:23: attempt to call method 'SetTeam' (a nil value)

Here is my code from shared.lua :

GM.Name     = "Test"
GM.Author     = "Jakfrost944"
GM.Email     = "N/A"
GM.Website     = "N/A"

DeriveGamemode( "sandbox" )

team.SetUp( 1, "Player", Color( 125, 125, 125, 225 ) )
team.SetUp( 2, "Juggernaut", Color( 225, 225, 225, 225) )

function team1( ply )

    ply:SetTeam( 1 )


concommand.Add( "team_1", team1 )

function team2( ply )

    ply:SetTeam( 2 )


concommand.Add( "team_2", team2 )

Here are the wiki pages for concommand.Add() and player.SetTeam() :

You’re calling player:SetTeam on the client. Make sure its run on the server (Put it in init.lua)

If I put this code in init.lua, then how will the client be able to run the command?

Commands on the server can be run by the client. Main way of networking data to the server.

I’m sorry, I’m confused. I’m still new to lua, obviously.


How can server commands be run by the client?

The server needs to know who is in what team and it’s alot easier for the server to distribute that information to all the players than for one client.

Server commands cane be run by the client either by calling console commands or using user messages which are essentially the same but user messages cannot be called randomly in the console by the user making them more secure. Also, all this is very well explained in the GmodWiki: