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:
And you will find your RapidAPI key here:
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
- Decoding JSON to Lua: HttpService | Documentation - Roblox Creator Hub
- Encoding Lua to JSON: HttpService | Documentation - Roblox Creator Hub
- HttpsService: HttpService | Documentation - Roblox Creator Hub