GetPlayerCount: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
m (→See Also) |
||
(26 intermediate revisions by 19 users not shown) | |||
Line 1: | Line 1: | ||
{{Server function}} | |||
__NOTOC__ | __NOTOC__ | ||
This function returns the number of players currently connected to the server. | This function returns the number of players currently connected to the server. | ||
{{Note|#getElementsByType("player") works the same as this function but also works client side unlike this function.}} | |||
==Syntax== | ==Syntax== | ||
<syntaxhighlight lang="lua"> | |||
int getPlayerCount ( ) | |||
</syntaxhighlight> | |||
{{OOP||[[Player]].getCount||}} | |||
===Returns=== | |||
Returns the number of players connected to the server as an [[int]]. | |||
==Example== | ==Example== | ||
This example displays a chat message with the number of players connected to the server when a player joins or quits. | |||
<syntaxhighlight lang="lua"> | |||
function playerCount ( ) | |||
outputChatBox ( "There are now " .. getPlayerCount() .. " players on this server!" ) | |||
end | |||
addEventHandler ( "onPlayerJoin", root, playerCount ) | |||
addEventHandler ( "onPlayerQuit", root, playerCount ) | |||
</syntaxhighlight> | |||
==Shared implementation of getPlayerCount== | |||
''getElementsByType("player")'' returns a regular-indexed table with every player connected to the server, so by counting how many entries has the table (by using the ''#'' operator) we can archieve the same result as this function, but also clientside. However, it's more efficient to use the built-in function serverside. The next function replaces this function accordingly serverside and clientside to work on both. | |||
<section name="Shared (client and server)" class="both" show="true"> | |||
<syntaxhighlight lang="lua"> | |||
local originalGetPlayerCount = getPlayerCount -- Store the original getPlayerCount function to a variable | |||
function getPlayerCount() | |||
-- If originalGetPlayerCount is defined, that means that this function is executed serverside. | |||
-- The next line returns the result of the original function if it's defined. If not, it counts the number of player elements (to also work clientside). | |||
return originalGetPlayerCount and originalGetPlayerCount() or #getElementsByType("player") | |||
end | |||
</syntaxhighlight> | |||
</section> | |||
==See Also== | |||
{{Player functions|server}} | |||
[[ru:getPlayerCount]] |
Latest revision as of 22:41, 6 September 2024
This function returns the number of players currently connected to the server.
Syntax
int getPlayerCount ( )
OOP Syntax Help! I don't understand this!
- Method: Player.getCount(...)
Returns
Returns the number of players connected to the server as an int.
Example
This example displays a chat message with the number of players connected to the server when a player joins or quits.
function playerCount ( ) outputChatBox ( "There are now " .. getPlayerCount() .. " players on this server!" ) end addEventHandler ( "onPlayerJoin", root, playerCount ) addEventHandler ( "onPlayerQuit", root, playerCount )
getElementsByType("player") returns a regular-indexed table with every player connected to the server, so by counting how many entries has the table (by using the # operator) we can archieve the same result as this function, but also clientside. However, it's more efficient to use the built-in function serverside. The next function replaces this function accordingly serverside and clientside to work on both.
Click to collapse [-]
Shared (client and server)local originalGetPlayerCount = getPlayerCount -- Store the original getPlayerCount function to a variable function getPlayerCount() -- If originalGetPlayerCount is defined, that means that this function is executed serverside. -- The next line returns the result of the original function if it's defined. If not, it counts the number of player elements (to also work clientside). return originalGetPlayerCount and originalGetPlayerCount() or #getElementsByType("player") end
See Also
- getAlivePlayers
- getDeadPlayers
- getPlayerACInfo
- getPlayerAnnounceValue
- getPlayerCount
- getPlayerIdleTime
- getPlayerIP
- getPlayerVersion
- getRandomPlayer
- isPlayerMuted
- redirectPlayer
- resendPlayerACInfo
- resendPlayerModInfo
- setPlayerAnnounceValue
- setPlayerMuted
- setPlayerScriptDebugLevel
- setPlayerTeam
- setPlayerVoiceBroadcastTo
- setPlayerVoiceIgnoreFrom
- setPlayerWantedLevel
- spawnPlayer
- takePlayerScreenShot
- Shared
- getPlayerTeam
- getPlayerBlurLevel
- setPlayerBlurLevel
- getPlayerSerial
- forcePlayerMap
- getPlayerScriptDebugLevel
- getPlayerFromName
- getPlayerMoney
- getPlayerName
- getPlayerNametagColor
- getPlayerNametagText
- getPlayerPing
- getPlayerWantedLevel
- givePlayerMoney
- isPlayerMapForced
- isPlayerNametagShowing
- setPlayerHudComponentVisible
- setPlayerMoney
- setPlayerNametagColor
- setPlayerNametagShowing
- setPlayerNametagText
- takePlayerMoney
- countPlayersInTeam
- getPlayersInTeam
- isVoiceEnabled
- setControlState
- getControlState