GetPlayerCount: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(Better explanation of the possible shared implementation of this function)
Line 21: Line 21:
</syntaxhighlight>
</syntaxhighlight>


==Clientside Example (Doesn't user getPlayerCount!)==
==Shared implementation of getPlayerCount==
This example uses the getElementsByType function to display the player count, same result but also works clientside!
''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="Client" class="client" show="true">
<section name="Shared (client and server)" class="both" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
addEventHandler( "playercount",
local originalGetPlayerCount = getPlayerCount -- Store the original getPlayerCount function to a variable
     function ()
 
        outputChatBox ( "There are now " .. #getElementsByType( "player" ) .. " players on this server!" )
function getPlayerCount()
    end
    -- 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>
</syntaxhighlight>
</section>
</section>

Revision as of 12:26, 9 July 2014

This function returns the number of players currently connected to the server.

[[{{{image}}}|link=|]] Note: #getElementsByType("player") works the same as this function but also works client side unlike this function.

Syntax

int getPlayerCount ( )

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", getRootElement(), playerCount )
addEventHandler ( "onPlayerQuit", getRootElement(), playerCount )

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.

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