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
Step 2
Link: http://ip-api.com/json/
So, basically what this link is, whoever clicks this click, you get the location and all info, try clicking it
So you got your info, after clicking it
Now you need to make roblox server open that link
The info you get in the link is an API and is a JSON table, we have to make it Lua table by using a https function
Here’s how:
local url = "http://ip-api.com/json/"
local httpsservice = game:GetService("HttpService")
task.wait(2) -- server loads fully
local getasyncinfo = httpsservice:GetAsync(url) -- roblox server will get info from that link, this will be in JSON format
local decodedinfo = https:JSONDecode(getasyncinfo) -- make the table into lua table, so its easy to read it
print("Server Location: "..decodedinfo["country"]) -- prints "Server location: India" for me
The decoded table have all these:
local table = {
["as"] = "Internet Name",
["city"] = "###",
["country"] = "####",
["countryCode"] = "#",
["isp"] = "###",
["lat"] = ###, -- number
["lon"] = 78.50749999999999,
["org"] = "###",
["query"] = "###",
["region"] = "###",
["regionName"] = "###",
["status"] = "success",
["timezone"] = "###",
["zip"] = ### - number
}
print(table["city"]) -- prints city
Step 3
Enjoy! The tutorial is done
Explanation
So, basically first player who joined the server, the server location will be the player location
So, you make a script that gets this location
Same like you got your location after clicking API,
This script will be the one clicking the API, so it gets its location and then you use the function string.sub
to get a certain part of the string, the info you see in the API, looks like a table but its a string
Errors (Optional, read if you have time)
If the https service, is not enabled or
the link is not pasted correctly or
any other error can break this server location script
If you would like to get a better version of this script, try this script below
local url = "http://ip-api.com/json/"
local httpsservice = game:GetService("HttpService")
game.Players.PlayerAdded:Connect(function(plr)
local success,errormessage = pcall(funtion() -- the script won't stop working at a line if its inside a pcall function
task.wait(2) -- player loads fully
local getasyncinfo = httpsservice:GetAsync(url) -- roblox server will get info from that link
game.Workspace.ServerLocation.Value = "Server Location: "..string.sub(tostring(getasyncinfo),31,37) -- 31 - 37 is is the country name in 5 words
end)
if errormessage ~= nil then -- if its not success script, it will print the error
print(errormessage)
end
end)
References
- Decoding JSON to Lua: HttpService:JSONDecode
- Encoding Lua to JSON: HttpService:JSONEncode
- HttpsService: HttpService