RedirectPlayer: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
 
(14 intermediate revisions by 8 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Server function}}
{{Server function}}
This function redirects the player to a specified server.<br>
This function redirects the player to a specified server.
{{Note|A resource using this function needs '''ACL rights''' in order to work (function.redirectPlayer)}}


==Syntax==
==Syntax==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool redirectPlayer ( player thePlayer, string serverIP, int serverPort, [ string serverPassword ] )
bool redirectPlayer ( player thePlayer, string serverIP = "", int serverPort = 0 [, string serverPassword = "" ] )
</syntaxhighlight>
</syntaxhighlight>
 
{{OOP||[[player]]:redirect||}}
===Required Arguments===
===Required Arguments===
*'''thePlayer:''' The player you want to redirect
*'''thePlayer:''' The player you want to redirect
*'''serverIP:''' The ip of the server you want to redirect the player to
*'''serverIP:''' The IP address (or domain name that resolves to the IP address) of the server you want to redirect the player to. '''Use an empty string to reconnect to the same server.'''
*'''serverPort:''' The game port of the server you want to redirect the player to
*'''serverPort:''' The game port of the server you want to redirect the player to, this is usually 22003. '''Set to zero to use the same port as the current server.'''


===Optional Arguments===
===Optional Arguments===
Line 19: Line 20:
Returns ''true'' if the player was redirected successfully, ''false'' if bad arguments were passed.
Returns ''true'' if the player was redirected successfully, ''false'' if bad arguments were passed.


==Example==  
==Example==
This example redirects the player to another server when they join.
 
This example auto-redirects all connecting players to another given servers' IP:port.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function joinserverHandlerFunction(playerSource)
local ip_port = "123.123.1.2:1234" -- enter IP and port in format: 192.168.1.1:22003
redirectPlayer (playerSource, "192.168.1.3", 22003, "potatoes") --redirect the player
local password = "password_placeholder" -- If the server is passworded insert password here (if no password, it wont use the value)
 
function onConnectRedirect()
redirectPlayer(source, gettok(ip_port,1,":"), tonumber(gettok(ip_port,2,":")), password)
end
end
addEventHandler ("onPlayerJoin", root, onConnectRedirect)
</syntaxhighlight>


addEventHandler("onPlayerJoin",root, joinserverHandlerFunction)  
This example adds a "joinserver" command using the syntax, "/joinserver serverIP serverPort [serverPassword]".
<syntaxhighlight lang="lua">
function joinserverHandlerFunction (playerSource, commandName, serverIP, serverPort, serverPassword)
if serverIP and serverPort then --if IP and Port were specified
if serverPassword then --if also a password was specified
redirectPlayer (playerSource, serverIP, tonumber(serverPort), serverPassword) --redirect the player
else -- else if no password was specified
redirectPlayer (playerSource, serverIP, tonumber(serverPort))  --redirect the player without using the serverPassword parameter
end
else -- if no IP or Port have been specified
outputChatBox ("Error! Correct Syntax: /joinserver IP Port [Password]", playerSource) --output an Error message to the chatbox
end
end
 
addCommandHandler ("joinserver", joinserverHandlerFunction)  
</syntaxhighlight>
 
{{New feature/item|3.0120|1.2|3429|
This example adds a "rejoin" command like the inbuilt reconnect command.
<syntaxhighlight lang="lua">
function rejoinMe(thePlayer, theCommand)
    redirectPlayer(thePlayer)
end
addCommandHandler("rejoin", rejoinMe) -- Attach rejoin command to our function
</syntaxhighlight>
</syntaxhighlight>
}}


==See Also==
==See Also==
{{Player_functions}}
{{Player functions|server}}
[[pt-br:RedirectPlayer]]

Latest revision as of 22:42, 6 September 2024

This function redirects the player to a specified server.

[[{{{image}}}|link=|]] Note: A resource using this function needs ACL rights in order to work (function.redirectPlayer)

Syntax

bool redirectPlayer ( player thePlayer, string serverIP = "", int serverPort = 0 [, string serverPassword = "" ] )

OOP Syntax Help! I don't understand this!

Method: player:redirect(...)


Required Arguments

  • thePlayer: The player you want to redirect
  • serverIP: The IP address (or domain name that resolves to the IP address) of the server you want to redirect the player to. Use an empty string to reconnect to the same server.
  • serverPort: The game port of the server you want to redirect the player to, this is usually 22003. Set to zero to use the same port as the current server.

Optional Arguments

  • serverPassword: The password for the server if it's protected

Returns

Returns true if the player was redirected successfully, false if bad arguments were passed.

Example

This example auto-redirects all connecting players to another given servers' IP:port.

local ip_port = "123.123.1.2:1234"	-- enter IP and port in format: 192.168.1.1:22003
local password = "password_placeholder" -- If the server is passworded insert password here (if no password, it wont use the value)

function onConnectRedirect()
	redirectPlayer(source, gettok(ip_port,1,":"), tonumber(gettok(ip_port,2,":")), password)
end
addEventHandler ("onPlayerJoin", root, onConnectRedirect)

This example adds a "joinserver" command using the syntax, "/joinserver serverIP serverPort [serverPassword]".

function joinserverHandlerFunction (playerSource, commandName, serverIP, serverPort, serverPassword)
	if serverIP and serverPort then --if IP and Port were specified
		if serverPassword then --if also a password was specified
			redirectPlayer (playerSource, serverIP, tonumber(serverPort), serverPassword) --redirect the player
		else -- else if no password was specified
			redirectPlayer (playerSource, serverIP, tonumber(serverPort))  --redirect the player without using the serverPassword parameter
		end
	else -- if no IP or Port have been specified
		outputChatBox ("Error! Correct Syntax: /joinserver IP Port [Password]", playerSource) --output an Error message to the chatbox
	end
end

addCommandHandler ("joinserver", joinserverHandlerFunction) 

This example adds a "rejoin" command like the inbuilt reconnect command.

function rejoinMe(thePlayer, theCommand)
    redirectPlayer(thePlayer)
end
addCommandHandler("rejoin", rejoinMe) -- Attach rejoin command to our function

See Also