Jump to content

[HELP] getServerIp return default value


MADD NØG

Recommended Posts

I'm having problems trying to use getServerIp() and it returns the default 127.0.0.1, because the function it's requesting is apparently starting before fetchRemote gets the IP, I tested it in a separate resource and it works fine, but I wanted to use everything in just one resource.

 

Example like this return the default

SERVER_IP = "127.0.0.1"

function getServerIp()
    return SERVER_IP
end

fetchRemote("http://checkip.dyndns.com/",
function (response)
	if response ~= "ERROR" then
		SERVER_IP = response:match("<body>Current IP Address: (.-)</body>") or "127.0.0.1"
	end
end)

function ipTesting()
	if connection then
	--local serverIp = getServerConfigSetting("serverip") // I don't understand why it doesn't return the real IP when the ip in mtaserver is set to auto (it would be less stressful getServerConfigSetting("serverip") to return the IP of the server even though it is set to auto)
	local serverIp = getServerIp()
	if serverIp then
            print(serverIp) --[[ Result 127.0.0.1 ]]--
        end
    end

 

When I add a SetTimer to wait for fetchRemote to return the value I get the server IP without any problems

Does anyone know of a way to resolve this? I specifically need to receive the IP when starting the resource and I was only able to do it through an external resource, I wanted to do everything in just one file, help me

 

Link to comment
  • Moderators
21 hours ago, Neto Silva said:

Does anyone know of a way to resolve this?

For the resource servertimesync, I am using this method to solve that problem.

 

1. I am using the function getServerTime to get the time, if available.

local timeNow = exports.servertimesync:getServerTime()

 

2. If it not available, then I am also listening to the event "onServerTimeInitialized", which is triggered when the data is available.

addEventHandler( "onServerTimeInitialized", root, function() end )

 

 

  • Thanks 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...