How to get list of countries (not their codes) using LocalizationService

Before any questions, I only need that because I’m creating a psychological horror game and this should be used for small easter egg part. So, now about the issues:

As far as everyone know, you can create player’s CODE by using :GetCountryRegionForPlayerAsync() in local script (for security reasons):

local LocalizationService = game:GetService("LocalizationService")
local player = game.Players.LocalPlayer

local result, code = pcall(function()
	return LocalizationService:GetCountryRegionForPlayerAsync(player)
end)

if result and code == "RU" then
	print("Hello, Russian!")
else
	print("GetCountryRegionForPlayerAsync failed: " .. code)
end

But! It returns player’s country code, which is not something that I want. I want to return players full country name instead, but still, for security reasons, keep that private (so locally). I have code kindly contributed by @iBuzzes that uses HttpRequest (which means that it should be used in a server script) and returns nil instead, this HttpRequest links to the site (http://country.io/names.json) that has country codes and their full names, but this code returns nil for me (once again probably because :GetCountryRegionForPlayerAsync() should be executed in LocalScript, which you can’t do with HttpRequests):

local Players = game:GetService("Players")
local HttpService = game:GetService("HttpService")
local LocalizationService = game:GetService("LocalizationService")
local Countries = {}

local success, result = pcall(function()
	return HttpService:GetAsync("http://country.io/names.json")
end)

if success and result then
	Countries = HttpService:JSONDecode(result)
end

Players.PlayerAdded:Connect(function(player)
	local success, code = pcall(LocalizationService.GetCountryRegionForPlayerAsync, LocalizationService, player)
	if success and code then
		print(Countries[code])
	end
end)

So, is there any other possible solutions, or something that I can do with the issue I have at the moment?

and no, i don’t want to code every country code and their full names by hand

Make the Http request from the server and store the result, and then get the client to request the result from the server via a remote function.

where to store the results? and how to compare results from the table exactly?

You will have decoded the JSON result into a table and already stored it on the server as
local Countries = {}
It looks like you have the code already there to do most the work its just splitting the client operation and server operation and linking the two with a remote function.
Unfortunately I don’t have time right now to go through in more detail.

I figured this out in the past, here is a dictionary with all the country codes to actual names:

local regionCountries = {
	["AD"] = "Andorra",
	["AE"] = "United Arab Emirates",
	["AF"] = "Afghanistan",
	["AG"] = "Antigua and Barbuda",
	["AI"] = "Anguilla",
	["AL"] = "Albania",
	["AM"] = "Armenia",
	["AN"] = "Netherlands Antilles",
	["AO"] = "Angola",
	["AQ"] = "Antarctica",
	["AR"] = "Argentina",
	["AS"] = "American Samoa",
	["AT"] = "Austria",
	["AU"] = "Australia",
	["AW"] = "Aruba",
	["AX"] = "Aland Islands",
	["AZ"] = "Azerbaijan",
	["BA"] = "Bosnia and Herzegovina",
	["BB"] = "Barbados",
	["BD"] = "Bangladesh",
	["BE"] = "Belgium",
	["BF"] = "Burkina Faso",
	["BG"] = "Bulgaria",
	["BH"] = "Bahrain",
	["BI"] = "Burundi",
	["BJ"] = "Benin",
	["BL"] = "Saint Barthelemy",
	["BM"] = "Bermuda",
	["BN"] = "Brunei Darussalam",
	["BO"] = "Bolivia",
	["BQ"] = "Bonaire, Saint Eustatius and Saba",
	["BR"] = "Brazil",
	["BS"] = "Bahamas",
	["BT"] = "Bhutan",
	["BV"] = "Bouvet Island",
	["BW"] = "Botswana",
	["BY"] = "Belarus",
	["BZ"] = "Belize",
	["CA"] = "Canada",
	["CC"] = "Cocos Islands",
	["CD"] = "Congo (DRC)",
	["CG"] = "Congo",
	["CH"] = "Switzerland",
	["CI"] = "Ivory Coast",
	["CK"] = "Cook Islands",
	["CL"] = "Chile",
	["CM"] = "Cameroon",
	["CN"] = "China",
	["CO"] = "Colombia",
	["CR"] = "Costa Rica",
	["CU"] = "Cuba",
	["CV"] = "Cape Verde",
	["CW"] = "Curaçao",
	["CX"] = "Christmas Island",
	["CY"] = "Cyprus",
	["CZ"] = "Czech Republic",
	["DE"] = "Germany",
	["DJ"] = "Djibouti",
	["DK"] = "Denmark",
	["DM"] = "Dominica",
	["DO"] = "Dominican Republic",
	["DZ"] = "Algeria",
	["EC"] = "Ecuador",
	["EE"] = "Estonia",
	["EG"] = "Egypt",
	["EH"] = "Western Sahara",
	["ER"] = "Eritrea",
	["ES"] = "Spain",
	["ET"] = "Ethiopia",
	["FI"] = "Finland",
	["FJ"] = "Fiji",
	["FK"] = "Falkland Islands (Malvinas)",
	["FM"] = "Micronesia",
	["FO"] = "Faroe Islands",
	["FR"] = "France",
	["GA"] = "Gabon",
	["GB"] = "United Kingdom",
	["GD"] = "Grenada",
	["GE"] = "Georgia",
	["GF"] = "French Guiana",
	["GG"] = "Guernsey",
	["GH"] = "Ghana",
	["GI"] = "Gibraltar",
	["GL"] = "Greenland",
	["GM"] = "Gambia",
	["GN"] = "Guinea",
	["GP"] = "Guadeloupe",
	["GQ"] = "Equatorial Guinea",
	["GR"] = "Greece",
	["GS"] = "South Georgia and the South Sandwich Islands",
	["GT"] = "Guatemala",
	["GU"] = "Guam",
	["GW"] = "Guinea-Bissau",
	["GY"] = "Guyana",
	["HK"] = "Hong Kong",
	["HM"] = "Heard Island and the McDonald Islands",
	["HN"] = "Honduras",
	["HR"] = "Croatia",
	["HT"] = "Haiti",
	["HU"] = "Hungary",
	["ID"] = "Indonesia",
	["IE"] = "Ireland",
	["IL"] = "Israel",
	["IM"] = "Isle of Man",
	["IN"] = "India",
	["IO"] = "British Indian Ocean Territory",
	["IQ"] = "Iraq",
	["IR"] = "Iran",
	["IS"] = "Iceland",
	["IT"] = "Italy",
	["JE"] = "Jersey",
	["JM"] = "Jamaica",
	["JO"] = "Jordan",
	["JP"] = "Japan",
	["KE"] = "Kenya",
	["KG"] = "Kyrgyzstan",
	["KH"] = "Cambodia",
	["KI"] = "Kiribati",
	["KM"] = "Comoros",
	["KN"] = "Saint Kitts and Nevis",
	["KP"] = "North Korea",
	["KR"] = "Korea",
	["KW"] = "Kuwait",
	["KY"] = "Cayman Islands",
	["KZ"] = "Kazakhstan",
	["LA"] = "Laos",
	["LB"] = "Lebanon",
	["LC"] = "Saint Lucia",
	["LI"] = "Liechtenstein",
	["LK"] = "Sri Lanka",
	["LR"] = "Liberia",
	["LS"] = "Lesotho",
	["LT"] = "Lithuania",
	["LU"] = "Luxembourg",
	["LV"] = "Latvia",
	["LY"] = "Libya",
	["MA"] = "Morocco",
	["MC"] = "Monaco",
	["MD"] = "Moldova",
	["ME"] = "Montenegro",
	["MF"] = "Saint Martin",
	["MG"] = "Madagascar",
	["MH"] = "Marshall Islands",
	["MK"] = "Macedonia",
	["ML"] = "Mali",
	["MM"] = "Myanmar",
	["MN"] = "Mongolia",
	["MO"] = "Macao",
	["MP"] = "Northern Mariana Islands",
	["MQ"] = "Martinique",
	["MR"] = "Mauritania",
	["MS"] = "Montserrat",
	["MT"] = "Malta",
	["MU"] = "Mauritius",
	["MV"] = "Maldives",
	["MW"] = "Malawi",
	["MX"] = "Mexico",
	["MY"] = "Malaysia",
	["MZ"] = "Mozambique",
	["NA"] = "Namibia",
	["NC"] = "New Caledonia",
	["NE"] = "FIX THIS",
	["NF"] = "Norfolk Island",
	["NG"] = "Nigeria",
	["NI"] = "Nicaragua",
	["NL"] = "Netherlands",
	["NO"] = "Norway",
	["NP"] = "Nepal",
	["NR"] = "Nauru",
	["NU"] = "Niue",
	["NZ"] = "New Zealand",
	["OM"] = "Oman",
	["PA"] = "Panama",
	["PE"] = "Peru",
	["PF"] = "French Polynesia",
	["PG"] = "Papua New Guinea",
	["PH"] = "Philippines",
	["PK"] = "Pakistan",
	["PL"] = "Poland",
	["PM"] = "Saint Pierre and Miquelon",
	["PN"] = "Pitcairn Islands",
	["PR"] = "Puerto Rico",
	["PS"] = "Palestine",
	["PT"] = "Portugal",
	["PW"] = "Palau",
	["PY"] = "Paraguay",
	["QA"] = "Qatar",
	["RE"] = "Reunion",
	["RO"] = "Romania",
	["RS"] = "Serbia",
	["RU"] = "Russian Federation",
	["RW"] = "Rwanda",
	["SA"] = "Saudi Arabia",
	["SB"] = "Solomon Islands",
	["SC"] = "Seychelles",
	["SE"] = "Sweden",
	["SG"] = "Singapore",
	["SH"] = "Saint Helena, Ascension and Tristan da Cunha",
	["SI"] = "Slovenia",
	["SJ"] = "Svalbard and Jan Mayen",
	["SK"] = "Slovakia",
	["SL"] = "Sierra Leone",
	["SM"] = "San Marino",
	["SN"] = "Senegal",
	["SO"] = "Somalia",
	["SR"] = "Suriname",
	["SS"] = "South Sudan",
	["ST"] = "Sao Tome and Principe",
	["SV"] = "El Salvador",
	["SX"] = "Sint Maarten",
	["SY"] = "Syria",
	["SZ"] = "Swaziland",
	["TC"] = "Turks and Caicos Islands",
	["TD"] = "Chad",
	["TF"] = "French Southern Territories",
	["TG"] = "Togo",
	["TH"] = "Thailand",
	["TJ"] = "Tajikistan",
	["TK"] = "Tokelau",
	["TL"] = "Timor-leste",
	["TM"] = "Turkmenistan",
	["TN"] = "Tunisia",
	["TO"] = "Tonga",
	["TR"] = "Turkey",
	["TT"] = "Trinidad and Tobago",
	["TV"] = "Tuvalu",
	["TW"] = "Taiwan",
	["TZ"] = "Tanzania",
	["UA"] = "Ukraine",
	["UG"] = "Uganda",
	["UM"] = "United States Minor Outlying Islands",
	["US"] = "United States",
	["UY"] = "Uruguay",
	["UZ"] = "Uzbekistan",
	["VA"] = "Holy See",
	["VC"] = "Saint Vincent and the Grenadines",
	["VE"] = "Venezuela",
	["VG"] = "Virgin Islands (British)",
	["VI"] = "Virgin Islands (US)",
	["VN"] = "Vietnam",
	["VU"] = "Vanuatu",
	["WF"] = "Wallis and Futuna",
	["WS"] = "Samoa",
	["YE"] = "Yemen",
	["YT"] = "Mayotte",
	["ZA"] = "South Africa",
	["ZM"] = "Zambia",
	["ZW"] = "Zimbabwe"
}
2 Likes

Notice: The country “NE” is filtered so it won’t let me put it in the code, it’s this country

The country’s name is filtered, it’s the country that sounds like Nigeria without the “ia” at the end.

2 Likes

Yeah, it’s fine by me! This is actually very helpful, thanks! :heart:

Just for the sake of a complete answer.
With HTTP enabled in game settings…
Server script:

local remoteFunction = game:GetService("ReplicatedStorage").RemoteFunction
local Countries = {}

local success, result = pcall(function()
	return HttpService:GetAsync("http://country.io/names.json")
end)

if success and result then
	Countries = HttpService:JSONDecode(result)
	print(Countries)
end

remoteFunction.OnServerInvoke = function()
	return Countries
end

Local Script:

local Players = game:GetService("Players")
local LocalizationService = game:GetService("LocalizationService")
local player = Players.LocalPlayer
local remoteFunction = game:GetService("ReplicatedStorage").RemoteFunction
local Countries = {}

local function GetCountryName()
	local success, code = pcall(LocalizationService.GetCountryRegionForPlayerAsync, LocalizationService, player)
	if success and code then
		Countries = remoteFunction:InvokeServer()
		print(code, "is the code for the country of", Countries[code])
	end
end

GetCountryName()

You could also do this just once, print to console then copy and paste the result into another script for use if you wanted to disable HTTP again.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.