game:GetHostLocation()


#1

As a Roblox developer, it is currently too hard to accurately get the hosting location of a Roblox server that is being played on.

If such a feature existed, it’d allow developers to do various things, such as improve matchmaking through use of prioritizing servers closer to the user, or even having certain builds/GUIs cater to the user in single-player games. More information/a proposed implementation can be found below.


Proposed Implementation

When the forum about the Server List Update was released, one of the answers to the FAQs was:

It would be useful if one of these tools allowed us to get the region in which the server is hosted. This can be used to allow players to join games in regions that are closest to them for optimal gameplay. Not only that, but it’d also allow developers to use this to customize certain aspects of their game.

Example: A single-player city game wants to customize the city based on where the server is located(Using “Nandos” as a fast-food resturant for a UK-based server versus using "Chick-Fil-a [:heart:] for US-based servers).

Here’s an example on how I was thinking it could be used/implemented:

local Region = game:GetHostLocation()

if Region == Enum.ServerHostRegion.USEast then
	print("Timezone: EST")
elseif Region == Enum.ServerHostRegion.USCentral then
	print("Timezone: CST")
end

And if desired, you could even take it a step further and have it return a special array with these values:

local HostData = game:GetHostLocation()

print(HostData.Country) --Prints "US"
print(HostData.Region) --Prints "East"

Of course all this can be subject to change, but overall I think it’d be a useful feature.


#2

Another cool feature that @Link150 and some other guys at the ScriptingHelpers Discord came up with was if we were able to get the relative location(similar to what this function would return) of the player. Features like these would really put a lot more power in the hands of developers.


#3

You can already do this with a fairly hacky method

Compare UNIX tick to the client tick. You then have the timezone diff (and can get the region).


#4

Have you actually checked that this works? My gut feeling would say this is incorrect as they made a promise that server time is more-or-less synchronised across all servers.


#5

It only works if you compare global Unix time to the client tickets, not server tick.


#6

But the offset will be the same for every server that that client connects to, since server times should be synchronized globally (tick() and os.time() should return similar values on all servers). You can estimate the timezone of the client but that doesn’t give you the region of the server.


#7

Can do a voting system. Assuming that Roblox’s matchmaking works properly, a majority of players should be in similar timezones and you should be able to make an educated guess as to where the server is located.


#8

Yeah, it’s not as good as having a location, but I don’t think ROBLOX will allow this for privacy issues

@antonio6643 Also, for future reference, I recommenced not suggesting the API. I recommend instead that you use the supplied format and list the problem, not a solution.

I don’t know if staff will even read this considering it isn’t in the format
¯\(ツ)


#9

Fair point. I edited the question a bit to follow the guidelines.


#10

I noticed that this game: https://www.roblox.com/games/503506257/Tales-from-the-Valley# was able to find the server host location and display it in the menu bar. Perhaps the creator @Arch_Mage could give us some insight on how he did it? :smiley:


#11

My guess is he GeoIPs the server address


#12

Yeah, as @Semaphorism suggested I just use a simple web service to retrieve information about the server.

Just send an HTTP GET request to this URL and it’ll give you enough details: http://ip-api.com/json/

Note that it isn’t always very accurate, however. e.g. it says Nevada for some servers even though Roblox doesn’t rent any servers in Nevada, etc.

Hope this helps. My personal opinion is that this API is unnecessary as there are plenty of web services that do the job, and having built-in API to broadcast this information isn’t great for Roblox’s server security.


#13

I actually would’ve never thought of this. You’ve just helped me a ton, thanks! :smiley:


#14

You can also use longitude to determine the servers location based on time zone.

local longitude = http:JSONDecode(http:GetAsync('http://ip-api.com/json/')).lon
				
local host = 'NA'
		
if(longitude>-180 and longitude<=-105)then
	host = 'WUS'
elseif(longitude>-105 and longitude<=-90)then
	host = 'CUS'
elseif(longitude>-90 and longitude<=0)then
	host = 'EUS'
elseif(longitude<=75 and longitude>0)then
	host = 'EU'
elseif(longitude<=180 and longitude>75)then
	host = 'AS'
end