How to get a server Location on Roblox

Hello Developers,

As you know, some games have Server Location display
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 you should get yourself a FREE API KEY from here. Get yourself a free subscription there and then you can start using your API.

You will find your API Key here:
image
And you will find your RapidAPI key here:
image
You need these two values, so copy them to the clipboard.

Step 3

Now you have your API Key of your own.
You will send a request to the API with this API key, now what the API will do is that whoever sends the requests or clicks the URL, their geolocation will be shown.

And now you will make the Roblox server click it, and you will get the data of its location, it is that easy!

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

Here’s how:

local HttpService = game:GetService("HttpService")

local url = "https://find-any-ip-address-or-domain-location-world-wide.p.rapidapi.com/iplocation"
local apikey = "12121212" -- replace this with your API key
local fullurl = url.."?apikey="..apikey -- your now concating the API key parameter in the url
local rapidapiKey = "12121212" -- replace with your Rapid API key
local headers = {
	["X-RapidAPI-Key"] = rapidapiKey,
}

local response = HttpService:GetAsync(fullurl, true, headers)

if response then
	local decodedResponse = HttpService:JSONDecode(response)
	print(decodedResponse["country"]) -- prints the server location country, there are a lot more, check them below
else
	print("Failed to fetch data")
end

The following data is received from the URL in JSON:

{
  "continent": "Asia",
  "country": "Singapore",
  "zipCode": "18",
  "accuracyRadius": 1000,
  "flag": "https://ipworld.info/static/flags/sg.png",
  "city": "Singapore",
  "timezone": "Asia/Singapore",
  "latitude": 1.3029,
  "countryGeoNameId": 1880251,
  "gmt": "(GMT+08:00) Singapore",
  "network": "0.0.00.0/10",
  "currencyName": "Singapore Dollar",
  "countryNativeName": "Singapore",
  "stateGeoNameId": null,
  "phoneCode": "+65",
  "state": null,
  "continentCode": "AS",
  "longitude": 103.857,
  "currencyNamePlural": "Singapore dollars",
  "cityGeoNameId": 1880252,
  "languages": "en",
  "numOfCities": 2,
  "org": "AMAZON-02",
  "ip": "000.0000.0000",
  "currencySymbol": "S$",
  "currencySymbolNative": "$",
  "isEU": "No",
  "countryTLD": ".sg",
  "countryCapital": "Singapore",
  "metroCode": null,
  "continentGeoNameId": 6255147,
  "stateCode": null,
  "countryISO2": "SG",
  "numOfStates": 5,
  "countryISO3": "SGP",
  "currencyCode": "SGD",
  "asNo": 00000,
  "status": 200
}

When you decode this, you get the same data but in a Lua readable dictionary.

Step 4

You made your server location script, you can now make your ideas based on server location come true.

Explanation

When the server is created, the API is called, and we won’t call the API again, because the server location of a server does not change unless the server is closed and new server is created in a different region.

So you would call the API once when the server is created.

Errors and Fixes

400 Bad Request
It occurs when the syntax is invalid or parameters required by API are not provided.

401 Unauthorized
When the API key or Rapid API key is not valid/not provided

403 Forbidden
It occurs when the request is received properly but the API refuses/fails to respond. This cannot be fixed by you, but one fix is firing the API once again after a few seconds.

404 Not Found
When the base URL is not valid/not proper/changed.
If the URL did not change on the website, it means you need to re-check your URL in your script.


References

Support Me

Paypal
buymeacoffee.com


170 Likes

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

8 Likes

The API takes your location from the browser, and timezone from your pc

Its not my API, so I am not sure why you got random country
You can try to find another API, as there are alot more API

6 Likes

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

7 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

5 Likes

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

3 Likes

Why use a PlayerAdded Event?
Its because when the player joins, they might be the first person, even thought they are not the first person, it won’t matter, as the server location does not change

Why isn't GetAsync, wrap in a pcall?
Its because, I want to make the script small, and not many developers know pcall, so if i keep it in script they would copy paste it, it would not be a tutorial

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

4 Likes

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

2 Likes

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.

13 Likes

Where i can find APIs , guys?..

3 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

6 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
5 Likes

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

2 Likes

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.

7 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.

3 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.

17 Likes

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

2 Likes