CountPlayersInTeam: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(Undo revision 56818 by Ahmedsayed Hamada (talk))
 
(14 intermediate revisions by 9 users not shown)
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
{{Server client function}}
This function is for returning the number of players in the specified team.
This function is for returning the number of players in the specified team.


Line 6: Line 7:
int countPlayersInTeam ( team theTeam )
int countPlayersInTeam ( team theTeam )
</syntaxhighlight>  
</syntaxhighlight>  
 
{{OOP||[[team]]:countPlayers|playerCount|}}
===Optional Arguments===  
===Optional Arguments===  
*'''theTeam:''' The team you wish to retrieve the player count of.
*'''theTeam:''' The team you wish to retrieve the player count of.


===Returns===
===Returns===
Returns an integer containing the number of players in the team.
Returns an integer containing the number of players in the team, ''false'' if it could not be retrieved.


==Example==
==Example==
<section name="Example 1" class="server" show="true">
This example adds a command in the console to find out how many players are on your team.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
addCommandHandler ( "teamsize", "outputTeamSize" )
function outputTeamSize ( source, commandName )
function outputTeamSize ( player )
-- Get player's team
-- Get player's team
local team = getPlayerTeam ( player )
local theTeam = getPlayerTeam ( source )
-- If the player is in any team
if theTeam 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 )
</syntaxhighlight>
</section>
<section name="Example 2" class="client" show="true">
This example adds a command in the console to find out how many players are on your team, clientside
<syntaxhighlight lang="lua">
function outputTeamSize ( commandName )
-- Get player's team
local theTeam = getPlayerTeam ( getLocalPlayer() )
-- If the player is in any team
-- If the player is in any team
if team then
if team then
-- Tell the player how big his team is
-- Tell the player how big his team is
outputChatBox ( "Your team has " .. countPlayersInTeam ( team ) .. " players.", player )
outputChatBox ( "Your team has " .. countPlayersInTeam ( theTeam ) .. " players." )
else
else
outputChatBox ( "You're not in a team.", player )
outputChatBox ( "You're not in a team." )
end
end
end
end
addCommandHandler ( "teamsize", outputTeamSize )
</syntaxhighlight>
</syntaxhighlight>
</section>
<section name="Example 3" class="server" show="true">
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
setPlayerTeam ( thePlayer , groveTeam ) --place the player in grove
elseif groveCount > ballasCount then --if there are more in grove
setPlayerTeam ( thePlayer , ballasTeam ) --place him in ballas
elseif groveCount < ballasCount then --if there are more in ballas
setPlayerTeam ( thePlayer , groveTeam ) --place him in grove.
end
end</syntaxhighlight>
</section>


==See Also==
==See Also==
{{Team functions}}
{{Team functions}}

Latest revision as of 23:59, 23 July 2018

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

Syntax

int countPlayersInTeam ( team theTeam )

OOP Syntax Help! I don't understand this!

Method: team:countPlayers(...)
Variable: .playerCount


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, false if it could not be retrieved.

Example

Click to collapse [-]
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 theTeam 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 )
Click to collapse [-]
Example 2

This example adds a command in the console to find out how many players are on your team, clientside

function outputTeamSize ( commandName )
	-- Get player's team
	local theTeam = getPlayerTeam ( getLocalPlayer() )
	-- 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." )
	else
		outputChatBox ( "You're not in a team." )
	end
end
addCommandHandler ( "teamsize", outputTeamSize )
Click to collapse [-]
Example 3

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
		setPlayerTeam ( thePlayer , groveTeam ) --place the player in grove
	elseif groveCount > ballasCount then --if there are more in grove
		setPlayerTeam ( thePlayer , ballasTeam ) --place him in ballas
	elseif groveCount < ballasCount then --if there are more in ballas
		setPlayerTeam ( thePlayer , groveTeam ) --place him in grove.
	end
end

See Also

Shared