Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I make a TextLabel show the server’s location?

Asked by
Abhorra 16
3 years ago

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.

0
I don't really see a reason to use this just to show it on a player's gui other for it to be "cool"? TheOnlineItalian213 99 — 3y
0
I found this on a devforum post ( https://devforum.roblox.com/t/game-gethostlocation/49488/3 ). The post itself is about proposing a new way to find the hosting location, but the comment here shows a method, by which you could achieve it https://prnt.sc/v9lks4 Gey4Jesus69 2705 — 3y
0
@TheOnlineItalian213: it's for a friend's commission. I could say I have no idea how to do it but I was curious to see how it could be possibly done. Besides, it's useful for PvP\FPS games where lag influences the game experience heavily. If I see the server host is too far away from me, I can just change server hoping to find one closer to me, which would result in less lag. Abhorra 16 — 3y
0
@Gey4Jesus69: yes, that's the thread I saw and helped quite a bit. I'm not really experienced though so I don't even know how to start with what the person in the screenshot suggested lol. Abhorra 16 — 3y
0
I personally find it rather scary that when you send a GET request to the JSON api that you've listed, you can get the exact location, down to the zip code. At any rate, that's how you'll have to do it (forget the longitudinal stuff) Gey4Jesus69 2705 — 3y

3 answers

Log in to vote
0
Answered by 3 years ago

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.

0
The "longitudinal math" in my answer is necessary if you want an easy way to determine the 'region' rather than the specific country. You could list the country as the server name but that might not be desired. In this case it is according to region such as Western US versus Eastern US. Therefore it entirely depends on the use case. dyler3 1510 — 3y
0
Oh, thank you so much for the code! I managed to make the one on the Devforum work but this one seems a lot more elegant. Abhorra 16 — 3y
0
Oh, thank you so much for the code! I managed to make the one on the Devforum work but this one seems a lot more elegant. By the way, i think you might have misunderstood the purpose of this request. I'm not trying to locate the Player position, but where the Server is situated. I have no interest, nor I want to break ROBLOX's ToS, in finding out where players are from. Abhorra 16 — 3y
0
Also, the reason why it displays your state when test-playing, is that while in Studio, your own computer is considered the server. While testing in the actual server, you are getting the location of where your server is hosted. Of course I might be wrong, but that's what I understood from the DevForum's threads regarding the topic. Once again, thank you for the code, I'll test it and let you know Abhorra 16 — 3y
View all comments (2 more)
0
LAST THING: I always try to understand any code I'm provided by others, so I have to ask: what are the numbers on line10 of the LocalScript? The ones between square brackets. Forgive my ignorance, I'm relatively new to LUA. Abhorra 16 — 3y
0
They're table indexes. It's really a separate discussion, but on line 10 of the other script, I saved the countryCode to the table, and, since the table was empty, it naturally set it to the first index. Same with the region, but it set it to the second, Gey4Jesus69 2705 — 3y
Ad
Log in to vote
0
Answered by
dyler3 1510 Moderation Voter
3 years ago
Edited 3 years ago

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.

0
You are correct but there is no need to use the longitude math used above since the API already returns the country and region. Benbebop 1049 — 3y
0
There is no need to do that "longitudinal math" as described on the devforum. See my answer Gey4Jesus69 2705 — 3y
0
Thank you! I was missing the variable part, I completely forgot it. HTTP requests were already enabled luckily. The script works pretty well, the longitude math is however a bit unnecessary as Bendebop said, the API already provides the info I need in detail. Abhorra 16 — 3y
Log in to vote
-1
Answered by
Benbebop 1049 Moderation Voter
3 years ago
Edited 3 years ago

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

0
Was it something I said? Benbebop 1049 — 3y
0
LOL Gey4Jesus69 2705 — 3y
0
Thank you for the answer! I appreciate the links on the documentation regarding GetAsync, HTTPService and JSONDecode, really useful. :) Abhorra 16 — 3y

Answer this question