CountPlayersInTeam: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
No edit summary
Line 14: Line 14:


==Example==
==Example==
'''Example 1:''' This example adds a command in the console to find out how many players are on your team.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function outputTeamSize ( source, commandName )
function outputTeamSize ( source, commandName )
Line 28: Line 29:
addCommandHandler ( "teamsize", outputTeamSize )
addCommandHandler ( "teamsize", outputTeamSize )
</syntaxhighlight>
</syntaxhighlight>
 
'''Example 2:''' This example balances a gamemode, to ensure equal number of players between the "grove" and "ballas" teams.  This could be triggered when a player joins the server, or for all players currently in the server when the gamemode starts.
<syntaxhighlight lang="lua">
function balanceTeams ( thePlayer )
--get the team pointers from their names
local groveTeam = getTeamFromName ( "grove" )
local ballasTeam = getTeamFromName ( "ballas" )
--count the number of players in each team, and store them
local groveCount = countPlayersInTeam ( groveTeam )
local ballasCount = countPlayersInTeam ( ballasTeam )
if groveCount == ballasCount then --if the teams are equal
addPlayerToTeam ( thePlayer , groveTeam ) --place the player in grove
elseif groveCount > ballasCount then --if there are more in grove
addPlayerToTeam ( thePlayer , ballasTeam ) --place him in ballas
elseif groveCount < ballasCount then --if there are more in ballas
addPlayerToTeam ( thePlayer , groveTeam ) --place him in grove.
end
end</syntaxhighlight>
==See Also==
==See Also==
{{Team functions}}
{{Team functions}}

Revision as of 20:59, 15 July 2007

This function is for returning the number of players in the specified team.

Syntax

int countPlayersInTeam ( team theTeam )

Optional Arguments

  • theTeam: The team you wish to retrieve the player count of.

Returns

Returns an integer containing the number of players in the team.

Example

Example 1: This example adds a command in the console to find out how many players are on your team.

function outputTeamSize ( source, commandName )
	-- Get player's team
	local theTeam = getPlayerTeam ( source )
	-- If the player is in any team
	if team then
		-- Tell the player how big his team is
		outputChatBox ( "Your team has " .. countPlayersInTeam ( theTeam ) .. " players.", source )
	else
		outputChatBox ( "You're not in a team.", source )
	end
end
addCommandHandler ( "teamsize", outputTeamSize )

Example 2: This example balances a gamemode, to ensure equal number of players between the "grove" and "ballas" teams. This could be triggered when a player joins the server, or for all players currently in the server when the gamemode starts.

function balanceTeams ( thePlayer )
	--get the team pointers from their names
	local groveTeam = getTeamFromName ( "grove" )
	local ballasTeam = getTeamFromName ( "ballas" )
	--count the number of players in each team, and store them
	local groveCount = countPlayersInTeam ( groveTeam )
	local ballasCount = countPlayersInTeam ( ballasTeam )
	if groveCount == ballasCount then --if the teams are equal
		addPlayerToTeam ( thePlayer , groveTeam ) --place the player in grove
	elseif groveCount > ballasCount then --if there are more in grove
		addPlayerToTeam ( thePlayer , ballasTeam ) --place him in ballas
	elseif groveCount < ballasCount then --if there are more in ballas
		addPlayerToTeam ( thePlayer , groveTeam ) --place him in grove.
	end
end

See Also