Convert Player Region ID into the associated Flag Emoji

Heya! This isn’t anything too impressive, but I thought it might help a few people out there hoping to improve the experience for their international audience.

Why I Made It

Recently I created a private server list mechanic for a superhero roleplay game of mine. It’s nothing too fancy, but when creating it I wanted to solve a problem that my players were having: Players who speak non-english languages were tired of being in servers where most of the people speak english as they couldn’t roleplay with most of them.

While it is admirable for Roblox to try to push for a utopian like world where people from across the world play side by side, it’s not there yet. What my band-aid solution to the problem was private servers, available to all no matter what the language, but with the language of the host displayed on the list to signal potential language compatibility. That left me with a question though - how do I best visually convey to people that a server is intended for a different language audience? Of course I could just put it in text, but when scrolling through a long list of servers it’s easy to miss that key information. So instead, I decided to go with the region, as with almost every country is a colorful flag emoji.

How it Works

After doing some research, I realized that Unicode 6.0 has most major flags already available, and upon further research I realized that not only were the flags available, but that the entire conversion from localization ID to flag emoji could be fully automated!

Firstly, you need to get the player’s country code from the client using GetCountryRegionForPlayerAsync, which should return a two letter region code.

From there, simply take each letter (I personally used string.sub to index the specific letters) and use string.gsub to replace them with their equivalent regional-indicator emoji - you may have seen these emojis before on discord, often used to spell words in the emote area. What you may not have known is that when you arrange those letters into the order of a region code, it automatically translates that into the country’s flag!

So US = 🇺 + 🇸 = :us:

If you google the resulting emoji, you’ll see it’s actually the flag emoji! From there all you need to do is automate the process, and take the resulting emoji and place it in a TextLabel / TextButton. You may now use the associated emoji to visually show where a player is located in the world!

Final Product

Here’s the result for my server list:

Here’s code that will automatically translate player region IDs to the associated flag emoji:

function getFlagEmoji()
	--dictionary of regional indicators indexable by the regular letter
	local regionalIndicators = {
		a = "🇦",
		b = "🇧",
		c = "🇨",
		d = "🇩",
		e = "🇪",
		f = "🇫",
		g = "🇬",
		h = "🇭",
		i = "🇮",	
		j = "🇯",
		k = "🇰",
		l = "🇱",
		m = "🇲",
		n = "🇳",
		o = "🇴",
		p = "🇵",
		q = "🇶",
		r = "🇷",
		s = "🇸",
		t = "🇹",
		u = "🇺",
		v = "🇻",
		w = "🇼",
		x = "🇽",
		y = "🇾",
		z = "🇿",
	}
	-- get region ID to translate into reg-ind characters
	local regionID = string.lower(game:GetService("LocalizationService"):GetCountryRegionForPlayerAsync(game.Players.LocalPlayer))
	local l1 = string.sub(regionID ,1,1) --get the first letter
	local l2 = string.sub(regionID ,2,2) --get the second letter
	return regionalIndicators[l1]..regionalIndicators[l2] --index the regional indicators for each letter, concatenating the result into a single string that is equivalent to the flag code
end

Hope this helps someone!
-CJ

39 Likes

Hi, tried the script but gives me the us flag even if i’m in italy

Is it possible your Roblox account is configured to American English?

Are you conducting this test in the game or using Roblox Studio?

I am using Roblox studio, is it different if I test in game?

You must be in-game for it to recognize your country, it seems to only output US in Roblox Studio.

2 Likes

I’m sorry for the necropost, but it doesn’t

STOP NECROPOSTING :angry:
no more necroposting

@LovreDev @IFearTheSky @water40q @V1SIONUSE Roblox Studio displays computer region, it is possible your computer is locked to US region or has US settings enabled. Roblox the videogame will display based on your IP (location). For example, I live in Canada yet my PC is from France so Roblox Studio displays France while Roblox the game displays Canada.

why was i pinged here? :angry: :angry: :angry: :angry:
i didnt need to get pinged here!!! wth!

Awesome article but question. I’ve heard this is against Roblox TOS is it really?

No, atleast I don’t think so, since Roblox officially supports it.

sure but like they allow alot of stuff however are we devs allowed to showcase others where the player is from? how about confidentiality? must be more researched imo

here’s the script: – Fetch the LocalizationService
local LocalizationService = game:GetService(“LocalizationService”)
local Players = game:GetService(“Players”)

– Function to convert a region code to a flag emoji
local function getFlagEmoji(regionCode)
– Validate the region code (must be a 2-letter code)
if #regionCode ~= 2 then
return nil
end

-- Convert the region code to uppercase
regionCode = string.upper(regionCode)

-- Convert the region code letters to flag emoji
local flagOffset = 127397
local firstChar = string.byte(regionCode, 1) + flagOffset
local secondChar = string.byte(regionCode, 2) + flagOffset

return utf8.char(firstChar) .. utf8.char(secondChar)

end

– Function to get a player’s country and convert it to a flag emoji
local function displayPlayerFlag(player)
– Get the player’s country region code using LocalizationService
local success, countryCode = pcall(function()
return LocalizationService:GetCountryRegionForPlayerAsync(player)
end)

-- Check if fetching the country code was successful
if success and countryCode then
    -- Convert the country code to a flag emoji
    local flagEmoji = getFlagEmoji(countryCode)
    if flagEmoji then
        print(player.Name .. "'s country flag emoji: " .. flagEmoji)
    else
        print(player.Name .. ": Could not convert country code to flag emoji.")
    end
else
    print(player.Name .. ": Failed to fetch the player's country code.")
end

end

– Loop through all players in the game and display their flag
for _, player in ipairs(Players:GetPlayers()) do
displayPlayerFlag(player)
end

– Listen for new players joining and display their flag as well
Players.PlayerAdded:Connect(function(player)
displayPlayerFlag(player)
end)

Valid point, but still, I don’t think it’s disallowed to do that. As you mentioned,