How to get a server Location on Roblox

I believe both of them are same
Can I know why you want to replace pcall(function() 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
2 Likes

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)
2 Likes

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

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

4 Likes

Oh sorry, but I will add it soon

3 Likes

Here is a sample this will print the country that the server is in:

local HttpService = game:GetService("HttpService")
local URL = "http://ip-api.com/json/"

local JSONServerData
local ServerData
local ServerCountry

local Success,ErrorMessage = pcall(function() -- the script won't stop working at a line if its inside a pcall function
	JSONServerData = HttpService:GetAsync(URL) -- roblox server will get info from that link
	ServerData = HttpService:JSONDecode(JSONServerData) -- make the json into lua table, so its easy to use
	ServerCountry = ServerData["country"] -- Get the country where the server is located
end)

if ErrorMessage ~= nil then -- if its not success script, it will print the error
	print(ErrorMessage)
else
	print(ServerCountry)
end

Sorry for the bump by the way I thought it would be helpful

10 Likes

I have tried this before with my game, it will return a false server location. You should use the timezone from the API instead of the city.

This is most likely how it is used in games:

local HttpService = game:GetService("HttpService")
local PlayerService = game:GetService("Players")

local API = "http://ip-api.com/json/"
local info = HttpService:JSONDecode(HttpService:GetAsync(API))

local function onPlayerAdded()
	local success, errorMessage = pcall(function()
		return
	end)

	if success then
		print("Server Location: "..tostring(info["timezone"]))
	end

	if errorMessage then
		warn(errorMessage)
	end
end

PlayerService.PlayerAdded:Connect(onPlayerAdded)

Server Outcome:
unknown

Here is another outcoome of when I used remotes and user interface:
unknown (1)

6 Likes

I’m not sure about it, because It always works for me, and this might be a aid to people who also got false location just like your faced problem, so anyway thanks for the info

1 Like

it worked, it isn’t false, maybe roblox assigned you a different server

2 Likes

Yes, also note that server location won’t always be your country, it sometimes changes location to different, when there is higher network traffic in a region, to avoid ping

2 Likes

I don’t really know where to find API, I found the API from someone’s post

However, you can try to find it here maybe
https://www.google.com/search?q=Free+weather+api&rlz=1C1ONGR_enIN965IN965&sxsrf=APq-WBtMSQAYZUvsKdAU2L6zH3oqVLvMXQ%3A1650789718379&ei=Vg1lYtXbFuzp2roP9ZqSmA4&ved=0ahUKEwjV3oz2pqz3AhXstFYBHXWNBOMQ4dUDCA4&uact=5&oq=Free+weather+api&gs_lcp=Cgdnd3Mtd2l6EAMyBwgAELEDEEMyBAgAEEMyBAgAEEMyBQgAEIAEMgoIABCABBCHAhAUMgQIABBDMgQIABBDMgUIABCABDIECAAQQzIFCAAQgAQ6BwgjEOoCECc6BAgjECc6CggAELEDEIMBEEM6BgguEAoQQzoICAAQgAQQsQM6BwgjELECECc6DQguELEDEIMBENQCEAo6DgguEIAEELEDEIMBENQCSgQIQRgASgQIRhgAUPwHWPceYNwgaANwAXgAgAGBAogBshWSAQYwLjE2LjKYAQCgAQGwAQrAAQE&sclient=gws-wiz

1 Like

None of the values are hash tagged, I don’t want others to know my location, so I made it #### in reality, it does not return any hashtag values

you should use camelCasing, 2. that gets the server location, so there is no reason to put a playerAdded event, 3. you should be using requestasync.

2 Likes

It displays IP Address of whoever who clicks the link, so we make server in Roblox click the link

Also I don’t know if your allowed to display zip code thats some personal info, players might also think the game is dangerous

Oh sorry I did not know I typed funtion instead of function, a “c” is missing, thx for telling

It would be nice, if you told it in a better way, why don’t you explain it? you just told typo

2 Likes

You are theroretically making a huge mistake:

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)

In this code you’re getting the server’s region every time a player joins, this is extraordinarily wasteful and if this practice were to be adopted in a large scale game(s), could potentially lead to a roblox-discord sorta situation where ip-api (the discord in this case) blocks or throttles requests made from roblox servers.

Instead, get this only once when the server starts up. Also, this information is not useful in studio and many developers do a lot of playtesting, so the request should ideally not go through in order to use the api less.

Also, it’s worth mentioning that ip-api also offers flags for the request url here. Which can be used to save resources on both roblox’s end and the API’s end, which contributes to the longevity of the API.

Here’s your script rewritten with a few useful things in mind:

if game:GetService('RunService'):IsStudio() then return end

local http = game:GetService('HttpService')
local whatever = workspace.ServerLocation

-- this url has flags that only return information that's specifically needed.
local url = "http://ip-api.com/json/?fields=16394"
local region_format = "%s, %s"

local response = http:JSONDecode(http:GetAsync(url))

if response.status == "success" then
    whatever.Value = string.format(
        region_format, 
        response.countryCode, 
        response.regionName
    )
end

-- Note that no error handling is required here.
5 Likes