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

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

Support me

Paypal

Contact me (if needed)

Discord


91 Likes

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

5 Likes

Welcome, glad to hear that from someone

3 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

4 Likes

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

5 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

4 Likes

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

2 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

2 Likes

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

3 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

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

9 Likes

Where i can find APIs , guys?..

2 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

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

6 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

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

13 Likes