How would I get a players countrys flag?

image
How would I go about getting in this image, the players country flag?
I don’t want how to do that in a playerlist, I just wanna know how to get the players flag so I can put it in my custom playerlist.

4 Likes

read this to get your information i guess

2 Likes

Its about how to get the players country, not its flag/region

2 Likes

If that script can get the country that’s all you need. You’ll have to fill in the rest yourself.

2 Likes

yeah but you can VERY easily match up flags with region by checking through a table with flag images
and seeing

if Country = v then
--get the image and stuff
end
1 Like

ok, were do I put that code at? I have tried that code before didn’t seem to work properly for me

1 Like

cant you just put it in a server script or something? i didnt make the solution to the post i dont know

1 Like

just an idea.
I lost where I got it from but I sourced most of it from the dev forum.

LOCALSCRIPT:

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

local flags = { -- what ever the region code is
["ca"] = "rbxassetid://12345",
["us"] = "rbxassetid://12345",
["mx"] = "rbxassetid://12345",
}

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

if result then
	print("Hello, "..code..flags[string.lower(code)])
else
	print("GetCountryRegionForPlayerAsync failed: " .. code)
end
2 Likes

I got it … give me a sec to put it together.
flag.rbxl (58.5 KB)

You’ll have to do the rest …
This will give you the country and a flag graphic.
Run this and check your output.

Jack lead me to it.

1 Like
local LocalizationService = game:GetService("LocalizationService")
local HttpService = game:GetService("HttpService")

local countryFlagJSON = "http://cdn.jsdelivr.net/npm/country-flag-emoji-json@2.0.0/dist/index.json"

local function countryCodeToEmoji(countryCode: string)
     local jsonTable = HttpService:GetAsync(countryFlagJSON)
     local countryTable = HttpService:JSONDecode(jsonTable)
     
     local emoji = ""
     for _, country in ipairs(countryTable) do
          if country.code == countryCode then
               emoji = country.code
          end
     end
     
     return emoji
end

-- pretend that 'player' exists and it is a Player object
local countryCode = LocalizationService:GetCountryRegionForPlayerAsync(player)
-- if player is in the United States, this will return "US"

print(countryCode) --> US
print(countryCodeToEmoji(countryCode)) --> 🇺🇸
1 Like

This code returns nil on line 15

1 Like

Tried this, its only for USA not other countries

1 Like

I tried this, doesn’t work at all neither does it print.

1 Like

Because you’re from America. If you were in some other place it would return that.

1 Like

I am from saudi arabia, riyadh not from america.

Well then …

Anyway there is a nice module in that script for the flag graphics.

cool, although i need to get the country for that

1 Like

I should post where I found that … someone did a lot of work. Should get the credit for it.
flag module

1 Like

Please if anyone has a solution, tell me I need to know how to get the players country and how to get the country image id from the module 2112jay sent.

This isn’t working?

local function getCountry()
    local HttpService = game:GetService("HttpService")
    local response = HttpService:GetAsync("https://ipinfo.io/json")
    local data = HttpService:JSONDecode(response)
    return data.country
end

The accuracy of this method depends on the player’s IP address and the data provided by the “ipinfo.io” service. It may not always accurately reflect the player’s actual physical location or country, as IP addresses can be associated with various locations, including VPN servers and proxies.