GetServerIp: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(10 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{Useful_Functions}}
<lowercasetitle/>
__NOTOC__
__NOTOC__
This function gets the server ip.
{{Client function}}
{{New feature/item|3.0161|1.6.0|22890|This function returns the IP of the server the client is currently connected to.}}
{{Note|On server-side there is the [[getServerIpFromMasterServer]] function, which might return the server IP under certain conditions.</br>You should consider using a third-party service via [[fetchServerIp]] ''(not an MTA function)'' if the previous function is unreliable for you.}}


==Syntax==
==Syntax==
<syntaxhighlight lang="lua">[string]string getServerIp( )</syntaxhighlight>
<syntaxhighlight lang="lua">
string getServerIp ( [ bool includePort = false ] )
</syntaxhighlight>
 
===Optional Arguments===
*'''includePort:''' If set to ''true'', the string will include the port of the server at the end of the string, after a colon (:) character.


===Required Arguments===
===Returns===
* none
A string containing the remote address of the server the client is currently connected to.


==Code==
==Example==  
<section name="Server" class="server" show="true">
This example creates a console command that outputs the server's IP to the chatbox.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
    function getServerIp()
function outputServerIp()
     callRemote("http://www.nub.hj.cx/getServerIp.php",function(value)
     outputChatBox("You are currently connected to ".. getServerIp(true))
    if value ~= "ERROR" then
end
      ip = value
    end
    end)
    return ip
    end
</syntaxhighlight>
</section>


==Example==
addCommandHandler("serverIp", outputServerIp)
<section name="Server" class="server" show="true">
<syntaxhighlight lang="lua">
    outputChatBox("the server ip is: "..getServerIp())
end)
</syntaxhighlight>
</syntaxhighlight>
</section>
Author: MJNONFIK


==See Also==
==See Also==
{{Useful_Functions}}
{{Utility_functions|client}}

Latest revision as of 20:07, 8 January 2025

ADDED/UPDATED IN VERSION 1.6.0 r22890:
This function returns the IP of the server the client is currently connected to.
[[{{{image}}}|link=|]] Note: On server-side there is the getServerIpFromMasterServer function, which might return the server IP under certain conditions.
You should consider using a third-party service via fetchServerIp (not an MTA function) if the previous function is unreliable for you.

Syntax

string getServerIp ( [ bool includePort = false ] )

Optional Arguments

  • includePort: If set to true, the string will include the port of the server at the end of the string, after a colon (:) character.

Returns

A string containing the remote address of the server the client is currently connected to.

Example

This example creates a console command that outputs the server's IP to the chatbox.

function outputServerIp()
    outputChatBox("You are currently connected to ".. getServerIp(true))
end

addCommandHandler("serverIp", outputServerIp)

See Also