Hello Developers, A couple of experiences/games have server region displays like Server Location: FL, USA, which shows the server location/region of the server.
In this tutorial, you’ll learn how to make one.
Step 1
Enable HTTPS requests in your game. Enable HttpService
Game Settings > Security > HTTPS Requests > Enable.
Step 2
Now we get the server's location data using an API.
Note: The data you get from the API request is in JSON. You cannot read that JSON data using a 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 the API is successfully retrieved and is not nil
local locationData = httpService:JSONDecode(response)
print(locationData) -- print the location data you retrieved from the 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: https://developer.roblox.com/en-us/api-reference/function/HttpService/JSONDecode Encoding Lua to JSON: https://developer.roblox.com/en-us/api-reference/function/HttpService/JSONEncode HttpService: https://developer.roblox.com/en-us/api-reference/class/HttpService