Team Balance Function

I hate asking you guys for help, but it seems both the wiki and my collections of gamemodes has failed me again.

Im working on a simple gamemode and have tried implimenting a what should be simple balance function that stops the player from joining a team with more players on it than the other team. Put simply a player can only join both teams if they are exactly balanced.

heres what im using (not very clever shoving it in both those functions I know, but it should work by rights.)
[lua]
function sb_team1( ply )
if team.NumPlayers(1) > team.NumPlayers(2) then
Player:ChatPrint( “Team Is Full” )
else
ply:UnSpectate()
ply:SetTeam( 1 )
ply:Spawn()
end
end

function sb_team2( ply )
if team.NumPlayers(2) > team.NumPlayers(1) then
Player:ChatPrint( “Team Is Full” )
else
ply:UnSpectate()
ply:SetTeam( 2 )
ply:Spawn()
end
end
concommand.Add( “sb_team1”, sb_team1 )
concommand.Add( “sb_team2”, sb_team2 )
[/lua]

Edit: Forgot to say, the problem is it simply doesnt work.

Thanks in advance.

Bump? Please I really want to finish this project before I start a new one.

You wrote Player instead of ply. Player doesn’t exist, in that context, obviously, so that’s why you get an error.
Also, you don’t need to have two functions that perform almost the same thing, a single one would do the job:

[lua]function sb_team(ply, cmd, args)
local t = args[1]
if t<1 or t>2 then
ply:ChatPrint( “Invalid team!” )
elseif team.NumPlayers(t) > team.NumPlayers(3-t) then
ply:ChatPrint( “Team Is Full” )
else
ply:UnSpectate()
ply:SetTeam(t)
ply:Spawn()
end
end

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

3-t because it returns the opposing team. If t=1, 3-t=2, and if t=2, 3-t=1. Simple isn’t it.
Write “sb_team 1” to join team 1, “sb_team 2” to join team2, obviously.

Also, indent your code, it’s much easier to read that way.

There are problems with your code, but the team setting works fine. Are you sure you are running it server side and not client side? I ran your script with lua_openscript on a listen server and then tried both commands and they properly set me to each team.

Follow _Kilburn’s suggestions and make sure you are running it server side.

How does the console command change between sb_team 1 and sb_team 2?

Also could you explain local t = args [1] ?

Sorry but this is all a bit out of my depth. But I need to learn.

Edit: and im running it in init.lua ( serverside from everything I know , unless im mistaken XD )
Edit Again: It works now :S, how very strange.

concommand.Add takes two arguments, a string, which is the name of the console command, and a function, which gets called when a player executes that command.
That function accepts three parameters, the first one (here, ply) is the player who called it, the second one (cmd) is the name of the command (completely useless most of the times), and the third one (args) is the list of arguments given.

So if you run sb_team 1, it will run the sb_team function with the parameters (your player entity, “sb_team”, {1}).
args[1] is the first parameter given to the console command, here it’s the team you want to join.
local t = args[1] simply creates a temporary variable, so instead of writing args[1] all the times, you just write t instead.

When hooked with a console command, a function receives the arguments player, command, and args. Player is the player entity that called the command. Command is the actual console command that was called. Args is a table of the arguments that were specified after the command. For example, in the default console command:


mp_timelimit 600

mp_timelimit would be the command. The arguments would be 600. If I specified my own command and called it as such:


my_command 20 "regan"

The command would be my_command and the arguments would be 20 and regan. Because args is a table, you need to use args[1] to access the first argument specified.

[editline]01:45PM[/editline]

God damn it.

Ohhhh, I just had a mega brainwave after that explanation and it all makes sense now. If you guys made a scripting help service I would pay for it. In fact you have helped me in a good few threads Yobdren. Someone should make you moderator for lua section or something.

Edit: One small problem. The args[1] is a string but obviously I cant perform math on a string. how can I convert (going to look at lua tutorials for a way)
Edit again: For anyone who will find this usefull theres a simple solution + 0 before asigning the value to the local t variable as this makes lua convert the string to an integer.

Heres what im using now thanks to your help.

[lua]
function BW_Team( ply, cmd, args )
local TeamInt = args[1] + 0
if TeamInt < 1 or TeamInt > 2 then
ply:ChatPrint( “Team Does Not Exist” )
elseif team.NumPlayers(TeamInt) > 1 and team.Numplayers(TeamInt) > team.NumPlayers(3-TeamInt) then
ply:ChatPrint( “Team Is Full” )
else
ply:UnSpectate()
ply:SetTeam(TeamInt)
ply:Spawn()
end
end

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

tonumber(args[1]) works well too. It’s longer than args[1]+0, but well, it’s easier to understand, right? Plus, it returns nil if the given value can’t be turned into a number, while args[1]+0 will give you an error.

Also, if any Lua helper became a moderator here, we would have one hell of a bunch of moderators.

True, but Yodbren seems fairly commited. Maybe they just have more free time XD. I hope to be at a point soon where I can help out a few newbies with lua too. Although asking questions that havent been answered helps as well right?