How to get a Server Location/Region on Roblox

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

1 Like

There are alot of APIs on the internet, I don’t have any link of that API, but I am sure there is one

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

14 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

1 Like

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.

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

2 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

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

16 Likes

Replace pcall(funtion() with pcall(function()

1 Like

Well, because funtion is a big typo :see_no_evil:

why did you edit your post lol

6 Likes

As others have stated, there is no point in using PlayerAdded for this, the server location won’t change so you only need a single request per server.
Also, you’ve said a “Java” table when you meant a JSON table.
I’ll leave a better formatted script below with good practices, feel free to edit your post and use it.

-- All you have to do is create a StringValue in ReplicatedStorage with name "ServerLocation"

local HttpService = game:GetService("HttpService")
local Location = game:GetService("ReplicatedStorage"):WaitForChild("ServerLocation")

local Success, Response = pcall(function()
	return HttpService:GetAsync("http://ip-api.com/json/")
end)

if Success then
	Response = HttpService:JSONDecode(Response)
	Location.Value = Response["country"]
end

You can also add more comments for beginners if you’d like to.

3 Likes

Can i ask what is this ? is it some kind of string value ?

game.Workspace.ServerLocation.Value
1 Like

Hi, there’s an easier way to do this by just requesting it from ipconfig.io/country.

local uri = "https://ipconfig.io/country"
local httpsservice = game:GetService("HttpService")

game.Players.PlayerAdded:Connect(function(plr)
    wait(2) -- player loads fully
    local getasyncinfo = httpsservice:GetAsync(uri)
    game.Workspace.ServerLocation.Value = "Server Location: "..getasyncinfo
end)
1 Like

Yes, its optional I just kept the server location string value in workspace

3 Likes

well this link is quite slow , it take like 10 second to get just 1 info . i recommended to use the first link they use .

5 Likes

Well, it does error if the value isn’t present, and you didn’t write anything about it in your topic either. This makes it extremely uncomfortable for less experienced programmers.

3 Likes