GetPlayerCount: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (Link to russian page)
(9 intermediate revisions by 7 users not shown)
Line 3: Line 3:
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.}}
{{Note|#getElementsByType("player") works the same as this function but also works client side unlike this function.}}
==Syntax==
==Syntax==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
int getPlayerCount ( )
int getPlayerCount ( )
</syntaxhighlight>
</syntaxhighlight>
 
{{OOP||[[Player]].getCount||}}
===Returns===
===Returns===
Returns the number of players connected to the server as an [[int]].
Returns the number of players connected to the server as an [[int]].
Line 22: Line 21:
</syntaxhighlight>
</syntaxhighlight>


==Example 2==
==Shared implementation of getPlayerCount==
<section name="Client" class="client" show="true">
''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.
This example command show your FPS.
<section name="Shared (client and server)" class="both" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local root = getRootElement()
local originalGetPlayerCount = getPlayerCount -- Store the original getPlayerCount function to a variable
local player = getLocalPlayer()
local counter = 0
local starttick
local currenttick


function fps ()
function getPlayerCount()
if not starttick then
    -- If originalGetPlayerCount is defined, that means that this function is executed serverside.
starttick = getTickCount()
    -- 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).
end
    return originalGetPlayerCount and originalGetPlayerCount() or #getElementsByType("player")
counter = counter + 1
end
currenttick = getTickCount()
if currenttick - starttick >= 1000 then
outputChatBox ( "Your FPS is: "..counter.."", player)
counter = 0
starttick = false
end
end
 
function command ()
addEventHandler("onClientRender", root, fps)
setTimer ( stop, 2000, 1)
end
addCommandHandler ( "myfps", command)
 
function stop ()
removeEventHandler ("onClientRender", root, fps)
end
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
Line 59: Line 37:
==See Also==
==See Also==
{{Player functions}}
{{Player functions}}
[[ru:getPlayerCount]]

Revision as of 14:53, 1 January 2015

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 ( )

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", 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