GetPlayerCount: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 21: Line 21:
addEventHandler ( "onPlayerQuit", getRootElement(), playerCount )
addEventHandler ( "onPlayerQuit", getRootElement(), playerCount )
</syntaxhighlight>
</syntaxhighlight>
==Clientside Example==
This example uses the getElementsByType function to display the player count, same result but also works clientside!
<section name="Client" class="client" show="true">
<syntaxhighlight lang="lua">
addEventHandler( "playercount",
    function ()
        outputChatBox ( "There are now " .. #getElementsByType( "player" ) .. " players on this server!" )
    end
)
</syntaxhighlight>
</section>


==See Also==
==See Also==
{{Player functions}}
{{Player functions}}

Revision as of 18:15, 17 April 2013

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 )

Clientside Example

This example uses the getElementsByType function to display the player count, same result but also works clientside!

Click to collapse [-]
Client
addEventHandler( "playercount",
    function ()
        outputChatBox ( "There are now " .. #getElementsByType( "player" ) .. " players on this server!" )
    end
)

See Also