GetPlayerPing: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
m (Added clarification that VPN usage won't give incorrect ping (tested))
 
(6 intermediate revisions by 6 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Server client function}}
{{Server client function}}
This function returns the ping of a specified [[player]]. The ping is the number of milliseconds that data takes to travel from the player's client to the server or the reverse.
This function returns the ping of a specified [[player]]. The ping is the number of milliseconds that data takes to travel from the player's client to the server or vice versa. If a player is using a VPN their ping will still be returned correctly.


==Syntax==
==Syntax==
<syntaxhighlight lang="lua">int getPlayerPing ( player thePlayer )</syntaxhighlight>
<syntaxhighlight lang="lua">int getPlayerPing ( player thePlayer )</syntaxhighlight>
 
{{OOP||[[player]]:getPing|ping|}}
===Required Arguments===
===Required Arguments===
*'''thePlayer''': The [[player]] whose ping you want to determine.
*'''thePlayer''': The [[player]] whose ping you want to determine.


===Returns===
===Returns===
Returns an ''int'' with the ping, or ''false'' if the player is invalid.
Returns the ping as an [[int]], or ''false'' if the player is invalid.


==Example==
==Example==
This example checks the ping of every player joining and warns him if it's over 100.
<section name="Server" class="server" show="true">
<section name="Server" class="server" show="true">
This example checks every players ping every 5 seconds and if it's over 500 they get kicked.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function checkPing()
function kickPing() -- Creates a function called kickPing
local ping = getPlayerPing(source) -- get the ping from the source element (the player who joined)
for i, player in ipairs(getElementsByType("player")) do -- Loop every player
if (ping > 100) then -- if its higher than 100..
if (getPlayerPing(player) >= 500) then -- If their ping is over 500
outputChatBox("Your ping is pretty high! Please try to lower it if possible.",source) -- output a message to the player
kickPlayer(player, "Ping over 500!") -- Kick them
end
end
end
end
end
addEventHandler("onPlayerJoin",getRootElement(),checkPing) -- add the event handler
setTimer(kickPing, 5000, 0) -- Every 5 seconds, the kickPing function is called.
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>


<section name="Client" class="client" show="true">
This example checks the ping of every player entering the 'ping' command and warns him if it's over 100.
This example checks the ping of every player entering the 'ping' command and warns him if it's over 100.
<section name="Client" class="client">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function checkPing()
function checkPing()
 
         local ping = getPlayerPing(localPlayer) -- get the ping from the source element (the player who joined)
         local ping = getPlayerPing(getLocalPlayer()) -- get the ping from the source element (the player who joined)
         if (ping > 100) then                         -- if it's higher than 100...
         if (ping > 100) then -- if its higher than 100..
                 outputChatBox("Your ping is pretty high! Please try to lower it if possible.") -- output a message to the player
                 outputChatBox("Your ping is pretty high! Please try to lower it if possible.") -- output a message to the player
         end
         end
end
end
addCommandHandler("ping",checkPing)
addCommandHandler("ping", checkPing)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
Line 42: Line 42:
==See Also==
==See Also==
{{Player functions}}
{{Player functions}}
[[ru:getPlayerPing]]

Latest revision as of 13:31, 15 September 2021

This function returns the ping of a specified player. The ping is the number of milliseconds that data takes to travel from the player's client to the server or vice versa. If a player is using a VPN their ping will still be returned correctly.

Syntax

int getPlayerPing ( player thePlayer )

OOP Syntax Help! I don't understand this!

Method: player:getPing(...)
Variable: .ping


Required Arguments

  • thePlayer: The player whose ping you want to determine.

Returns

Returns the ping as an int, or false if the player is invalid.

Example

Click to collapse [-]
Server

This example checks every players ping every 5 seconds and if it's over 500 they get kicked.

function kickPing() -- Creates a function called kickPing
	for i, player in ipairs(getElementsByType("player")) do -- Loop every player
		if (getPlayerPing(player) >= 500) then -- If their ping is over 500
			kickPlayer(player, "Ping over 500!") -- Kick them
		end
	end
end
setTimer(kickPing, 5000, 0) -- Every 5 seconds, the kickPing function is called.
Click to collapse [-]
Client

This example checks the ping of every player entering the 'ping' command and warns him if it's over 100.

function checkPing()
        local ping = getPlayerPing(localPlayer)  -- get the ping from the source element (the player who joined)
        if (ping > 100) then                          -- if it's higher than 100...
                outputChatBox("Your ping is pretty high! Please try to lower it if possible.") -- output a message to the player
        end
end
addCommandHandler("ping", checkPing)

See Also