So, I’ve seen some PvP\FPS games having a GUI element displaying - what I assume to be - the server’s location. Example here.
From what I’ve seen on the DevForum, the location is picked based on the first player to join the server. For example, if the first player joining is from Europe, the server will be located somewhere in Europe, or if the first player joining is from the US, the server will be located in the US (duh lol).
I’ve tried searching on the DevForum, Wiki, and Google, but nothing came up. Someone in a thread suggested using JSON code and do something like this:
local longitude = http:JSONDecode(http:GetAsync('http://ip-api.com/json/')).lon local host = 'NA' if(longitude>-180 and longitude<=-105)then host = 'WUS' elseif(longitude>-105 and longitude<=-90)then host = 'CUS' elseif(longitude>-90 and longitude<=0)then host = 'EUS' elseif(longitude<=75 and longitude>0)then host = 'EU' elseif(longitude<=180 and longitude>75)then host = 'AS' end
The most I can do is making the if loop changing the TextLabel's text but I have no idea how to grab the server location.
Sorry to add a third answer, but I've been fiddling with this since I commented earlier. Basically, I collected the JSON information when the game loaded, and I requested it by the client to update a GUI. Make sure HTTP services are enabled.
Server Script
local HTTPService = game:GetService("HttpService") local ReplicatedStorage = game:GetService("ReplicatedStorage") local Remote = ReplicatedStorage:WaitForChild("RemoteFunction") local Info = HTTPService:JSONDecode(HTTPService:GetAsync('http://ip-api.com/json/')) local function ReturnServerInfo(player) local ServerData = {} for i,v in pairs(Info) do if i == "countryCode" then table.insert(ServerData,v) elseif i == "region" then table.insert(ServerData,v) end end return ServerData end Remote.OnServerInvoke = ReturnServerInfo
Local Script
if not game:IsLoaded() then game:IsLoaded():Wait() end local ReplicatedStorage = game:GetService("ReplicatedStorage") local Remote = ReplicatedStorage:WaitForChild("RemoteFunction") local HostingInfo = Remote:InvokeServer() script.Parent.Text = HostingInfo[1] .. ", " .. HostingInfo[2]
Interestingly, it works just fine in studio mode (says the region is OH, which is the state in which I live).
However, in an actual server, it returns the region as Virginia. I found this kind of odd, but maybe that's an acceptable discrepancy for you. At any rate, the country is still accurate.
Your code is just about there. For this to work, firstly you need to go into your Game Settings and enable Allow Http Requests. This will allow the HttpService to make the GetAsync API request.
It is also worth mentioning that this will only work in a server script, NOT a local script. I will explain this at the end.
Now all you need to do is add this variable assignment above your code so the script knows what the http
variable is:
local http = game:GetService("HttpService")
That should be it. I tested the script and was able to get it working.
You should know that this is going to be returning the servers location directly, because it is inside a server script rather than a localscript on the client. No trickery with getting the location of a players computer. I suppose that is one of the reasons ROBLOX doesn't allow you to execute HTTP requests from the client.
The API you requested provides more information that you could easily use to find the location.
When I request it in my browser I get the following
{"status":"success","country":"Canada","countryCode":"CA”,"region":"~~”,"regionName":"~~","city":"~~","zip":"~~","lat":~~,"lon":~~,"timezone":"~~","isp":"~~","org":"~~","as":"~~","query":“~~"}
Sensitive information obfuscated for obvious reasons
From what the DevForm discussion you referenced says I assume you are supposed to call this server side and use the country and region value to display what the region is.
I’m alittle rusty with this stuff and I’m on mobile so excuse any mistakes I may make
local HTTPService = game:GetService(“HTTPService”) local areaInfo = HTTPService:GetAsync(“http://ip-api.com/json/“) local location = areaInfo[“country”] local region = areaInfo[“region”]
location
should contain the county of the server and region
should contain the province/state. You may need to JSONDecode areaInfo first, I’m not too sure, you will have to test it and see.
Make sure that HTTP Requests are enabled in the game settings.
Documentation on GetAsync and HTTPService