How to get a Server Location/Region on Roblox

Hello Developers,

Some experiences have server region displays like Server Location: FL, USA, which shows the server location/region of the server.

In this tutorial, you’ll know how to make that.

Step 1

Enable HTTPS requests in your game Enable HttpsService
Game Settings > Security > Https Requests > Enable

Step 2

Now we get the location data of the server using an API.

Note:
The data you get from the API request is in JSON. You cannot read that data using scripts until you convert it to a Lua Dictionary.

Here’s how you use the API to get location data:

local httpService = game:GetService("HttpService")
local url = "https://ipconfig.io/json"
local response = httpService:GetAsync(url, false) -- get the data from the API

if response then -- if the data from API is successfully retrieved and is not nil
	local locationData = httpService:JSONDecode(response)
	print(locationData) -- print the location data you retrieved from API
else
	print("Failed to fetch data from API.")
end

The data retrieved from the API is as follows: (example of location data if printed)
Note that the example shown below is a JSON dictionary and not Luau, you get a Luau Dictionary when you print it, the below is an example of data before it is decoded into Luau from JSON.

{
  "status": "success",
  "country": "United States",
  "countryCode": "US",
  "region": "FL",
  "regionName": "Florida",
  "city": "Miami",
  "zip": "33132",
  "lat": 25.7838,
  "lon": -80.1823,
  "timezone": "America/New_York",
  "isp": "Verizon Ltd",
  "as": "Verizon Limited",
  "query": "00.0.0000.000"
}

Step 3

Now you have your location data, but if you want to show it like (CA, US) or in a format you want.
You can use the keys from the location data for example:

print(locationData["country"]) -- this will print the country
print(locationData["region"]) -- this will print the region

Similarly, you can get the location and region in whatever format you want and whatever details you want.

Tutorial is done.


References

or refer below for more ways to get location data.

Single Line Code

Type this anywhere in the code to access the module: (up-to-date version)

local module = require(18156818435)
print(module.getLocation()) -- prints the location

Support Page

184 Likes

Thanks!, this is uself, i used it right now.

8 Likes

So it is proven that it works with vpn, great.

6 Likes

Thank you, also the server location is the first joined player location, so try to run the script only once because you might cause errors, I am sure you won’t but just for keeping game from extra memory lag

4 Likes

A few questions,
Why use a PlayerAdded Event?
Why isn’t GetAsync, wrap in a pcall?

2 Likes

Why use a PlayerAdded Event?
Fixed the tutorial so it’s not inside a player added event anymore.

Why isn't GetAsync, wrap in a call?
It is because I was aiming to make the tutorial as easy as possible, and not many developers know pcall.

However, good scripters know when to add pcalls, so they will do it even if I don’t keep it in the tutorial

3 Likes

I was just thinking about this! Now, are there any free weather APIs I can use with this tool?

1 Like

There are alot of APIs on the internet, I don’t have any link of that API, but I am sure there is one

3 Likes

You should always show the best practice regardless if you care about it or not there are a lot of new developers that may benefit from it.

14 Likes

Where i can find APIs , guys?..

2 Likes

So? How are players related to a server’s location?

You’re already telling them to just copy and paste a script into their game, without explaining anything

5 Likes

You don’t need to use string.sub for this situation. The URL returns a table which contains the stats of IP. Instead, use .country with JSONDecode function. Also it would be odd if you were to use only the first five letters of the country.

game.Workspace.ServerLocation.Value = "Server Location: ".. httpsservice:JSONDecode(getasyncinfo).country
4 Likes

Yes, I tried that first, turns out its a string after many errors

1 Like

Hello @Marimariius ,
I never told you need to copy paste the script, its a small script and have a small explanation below

You might get how the script works, after reading full post

2 Likes

Ah yes, dropping a full script and telling people “that’s how” is totally not telling them to copy and paste. Cool excuse.

6 Likes

It shouldn’t have returned a string.


game:GetService("HttpService"):GetAsync(something)

This retrieves the raw form of the thing you want to achieve.

local returnedInfo = game:GetService("HttpService"):GetAsync(something)

game:GetService("HttpService"):JSONDecode(returnedInfo)

And this one, converts the table, which is formatted in JSON type, to a Lua-formatted table.

2 Likes

I did not keep a full script, and its a small script and it tells how it works

if your not interested in this topic, please don’t spread bad replies
I am just trying to teach some people how do games get server location

5 Likes

Wait what, a Java table? :face_with_monocle:

So, I’ve started the search on what a “Java table” is, and came across this:

Object[][] data = {
    {"Mary", "Campione",
     "Snowboarding", new Integer(5), new Boolean(false)},
    {"Alison", "Huml",
     "Rowing", new Integer(3), new Boolean(true)},
    {"Kathy", "Walrath",
     "Knitting", new Integer(2), new Boolean(false)},
    {"Sharon", "Zakhour",
     "Speed reading", new Integer(20), new Boolean(true)},
    {"Philip", "Milne",
     "Pool", new Integer(10), new Boolean(false)}
};

Interesting, don’t you mean a JSON table?

I live in The Net her lan ds. Ahh, indeed, 5 words :stuck_out_tongue_winking_eye:

Ah, a newly released service, the HttpsService :thinking:

Not going to start a whole topic about why you should use GetService("Players"), but I can’t understand why you use PlayerAdded. I mean, the server location doesn’t change when someone joins (even if it’s the first user), that would be very strange…

Okay, thanks for acknowledging that it was nonsense to use PlayerAdded, but isn’t it an idea to fix an issue when you acknowledge an issue exists?

Don’t let this discourage you to contribute to the community. Consider dedicating more time to learn Luau.

16 Likes

Replace pcall(funtion() with pcall(function()

1 Like

Well, because funtion is a big typo :see_no_evil:

why did you edit your post lol

6 Likes