Ok so I have been trying to be able to get server location of the instence. I followed Wood’s tutorial and other tutorials, They mostly worked on studio enviorement but when I actually publish and go on to the game, it just refuses to work no matter what I do. I tried to put it in a looping pcall, and more, yet it did not work, so help.
This below is the script;
local function GetPlace()
print("running")
local suc, getIp = pcall(function()
return game.HttpService:GetAsync("https://ipconfig.io/json")
end)
local serverIP
if getIp then
local ipData = game.HttpService:JSONDecode(getIp)
serverIP = ipData["ip"]
else
task.wait(1)
GetPlace()
end
local _, ServerData = pcall(function()
return game.HttpService:GetAsync("https://ipapi.co/"..serverIP.."/json/")
end)
if ServerData then
print(ServerData)
ServerData = game.HttpService:JSONDecode(ServerData)
game.ServerStorage.ServerInfo.ServerLocation.Value = ServerData["region"]..", "..ServerData["country_code"]
else
task.wait(1)
GetPlace()
end
end
Do you have Allow HTTP Requests enabled on the published place? Is an error happening, where is the error?
Yes Its enabled
Bad Gateway Error but no matter what I do, I cant get around it
This is the API I currently use for this task:
--Fields the API returns, helps with autocomplete
type serverGeolocation = {as: string, asname: string, city: string, continent: string, continentCode: string,
country: string, countryCode: string, currency: string, district: string, hosting: boolean, isp: string,
lat: number, lon: number, mobile: boolean, offset: number, org: string, proxy: boolean, query: string,
region: string, regionName: string, status: string, timezone: string, zip: string}
local function getServerGeolocation(): serverGeolocation
local url = "https://demo.ip-api.com/json?fields=66842623"
local success, response = pcall(function()
return game.HttpService:GetAsync(url, nil, {origin = "https://ip-api.com"})
end)
if not success then
warn(response)
task.wait(1)
return getServerGeolocation()
end
local data = game.HttpService:JSONDecode(response)
if data.status ~= "success" then
warn("Failed to fetch server geolocation")
task.wait(1)
return getServerGeolocation()
end
return data
end
print(getServerGeolocation())
It returns in depth information about the server so I decided to share it.
The query
field has the server IP.