GetPlayerPing: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
Line 14: Line 14:
==Example==
==Example==
<section name="Server" class="server" show="true">
<section name="Server" class="server" show="true">
This example checks the ping of every player joining and warns him if it's over 100.
This example checks thePlayer's ping, and if it's over 500, kicks him/her.
<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)
if (getPlayerPing(thePlayer) >= 500) then -- If thePlayer's ping is over 500
if (ping > 100) then                 -- if it's higher than 100...
kickPlayer(thePlayer, "Ping over 500!") -- Kick them
outputChatBox("Your ping is pretty high! Please try to lower it if possible.", source) -- output a message to the player
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>

Revision as of 02:24, 14 April 2014

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.

Syntax

int getPlayerPing ( player thePlayer )

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 thePlayer's ping, and if it's over 500, kicks him/her.

function kickPing() -- Creates a function called kickPing
	if (getPlayerPing(thePlayer) >= 500) then -- If thePlayer's ping is over 500
		kickPlayer(thePlayer, "Ping over 500!") -- Kick them
	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(getLocalPlayer())  -- 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