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 thatJSON
data using Luau script unless it is converted to Luau readable data.
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
- Decoding JSON to Lua: HttpService | Documentation - Roblox Creator Hub
- Encoding Lua to JSON: HttpService | Documentation - Roblox Creator Hub
- HttpsService: HttpService | Documentation - Roblox Creator Hub
or you may implement any of the following (easier & quick):
Using the library
Type this anywhere in the code to access the module: (up-to-date version)
local module = require(18156818435)
print(module.getLocation()) -- prints the location