• Max amount of players on one team
    12 replies, posted
I am fairly new to lua and am wondering how to set the max amount of players on one team? thanks :D [editline]01:54AM[/editline] Any ideas? I'm working on a gamemode, and I'm pretty sure setting a max amount of players has something to do with setting the team to unjoinable? Not exactly sure :/
[url]http://wiki.garrysmod.com/?title=Team.NumPlayers[/url] Create an if statement to see whether it is equal or over a certain amount of players, if it is, don't allow them to join the team. [code] if (team.NumPlayers(1) >= 1) then return false else return true end; [/code]
[QUOTE=Mistur Man;18039765][url]http://wiki.garrysmod.com/?title=Team.NumPlayers[/url] Create an if statement to see whether it is equal or over a certain amount of players, if it is, don't allow them to join the team. [code] if (team.NumPlayers(1) >= 1) then return false else return true end; [/code][/QUOTE] How do you make a team not joinable?
When a player wants to join that team simply return false. You could check some conditions to see if you want to allow that player in or not.
[QUOTE=Crazy Quebec;18048158]When a player wants to join that team simply return false. You could check some conditions to see if you want to allow that player in or not.[/QUOTE] Ah, thanks alot. Also, lets say my code was [CODE] function CA( ply ) if (team.NumPlayers(1) >= MaxCA) then ply:PrintMessage( HUD_PRINTTALK, "That team is full!" ) return false else ply:SetTeam( 4 ) ply:Spawn() end end [/CODE] How would i make a command that would be typed in the console to set the value of MaxCA? Thanks :D
[lua] maxCA = 3 -- Give it a starting value local function SetMaxCA(ply,cmd,args) local number = args[1] if number and (number >= 0) and ply:IsAdmin() then maxCA = number end end concommand.Add("setmaxca",SetMaxCA)[/lua] :eng101: That should work, completely untested tough.
[QUOTE=Crazy Quebec;18048700][lua] maxCA = 3 -- Give it a starting value local function SetMaxCA(ply,cmd,args) local number = args[1] if number and (number >= 0) and ply:IsAdmin() then maxCA = number end end concommand.Add("setmaxca",SetMaxCA)[/lua] :eng101: That should work, completely untested tough.[/QUOTE] i get the error message attempt to compare number with string
Hmm, what did you enter? :confused: If it's a number it should work. The syntax is : setmaxca # Oh you probably need to do this : local number = tonumber(args[1])
[QUOTE=Crazy Quebec;18049176]Hmm, what did you enter? :confused: If it's a number it should work. The syntax is : setmaxca # Oh you probably need to do this : local number = tonumber(args[1])[/QUOTE] Thanks man it works! :D But, just wanted to ask (because I want to become better at lua) what exaclty is the tonumber for? :S
The tonumber I believe converts the number in the string to a number in code...I dunno, I'm just as new as you are :P.
I'll give a little explanation. The function converts a string to a number, for example "1" would be a string, and 1 would be a number, so it takes the "1" out of the string and converts it to a number which you can perform arithmatic on, the string "1" could be "Sausages", however obviously tonumber would not work, since it's a word. I hope that's cleared things up a little, and not just made matters worse.
Thanks to both of you, it does clear things up for me :D
To add a bit : tonumber() will also return nil when we give it anything else then a number. so doing if number (in my code snippet) will effectively check if it's a real number or not.
Sorry, you need to Log In to post a reply to this thread.