How do i set the image label to the flag of the country that the player is from

the title says it all, i basically made a player list, and when i click on a button of a specific player, an image label appears with the flag of the country that the player is from.
i kinda know how localization service works infact i do know how to get the country region for the player but the problem is that i dont know how to get the player, since the name of the player is wrote on the text button when a player joins.
Do any of you know how to make it work?

2 Likes

to get the country/region code of the player using the “GetCountryRegionForPlayerAsync” function. This returns a country/region code string

local player = game.Players.LocalPlayer
local countryRegionCode = player:GetCountryRegionForPlayerAsync()

local flagImageURL = "" -- Set the URL or asset ID of the flag image based on the country/region code

local imageLabel = script.Parent -- Replace "script.Parent" with the path to your ImageLabel object
imageLabel.Image = flagImageURL

Another way to do this could be by looping through all the players

for _,player in pairs(game:GetService("Players"):GetPlayers()) do
 local countryRegionCode = player:GetCountryRegionForPlayerAsync()

local flagImageURL = "" -- Set the URL or asset ID of the flag image based on the country/region code

local imageLabel = script.Parent -- Replace "script.Parent" with the path to your ImageLabel object
imageLabel.Image = flagImageURL
end
1 Like

yeah thats what i did to set the flag emoji, the problem is that i dont know how to make it work since i want the image label to show the flag WHEN i click the text button in which theres the player name in it

1 Like

can you show your explorer please?

EDIT: im going to recreate this just for testing so the file would be better :pensive:.

1 Like

Its most likely all from starter gui



alright im going to test what i just copied from you :skull:

1 Like

well i mean it doesnt work lmao

1 Like

can you please send that in a message i am not copying that :pensive:

script.Parent.MouseButton1Click:Connect(function()
	local Localization = game:GetService("LocalizationService")
	local CountryFlag = script.Parent.Parent.Parent.CountryFlag
	
	--how do i get the player that is associated from the text button??
	
			local result, code = pcall(function()
				return Localization:GetCountryRegionForPlayerAsync() -- the player
			end)
			
			if code == "DK" then
				CountryFlag.Image = "rbxassetid://105525969"
			elseif code == "US" then
				CountryFlag.Image = "rbxassetid://941761111"
			elseif code == "GB" then
				CountryFlag.Image = "rbxassetid://1064519589"
			elseif code == "CA" then
				CountryFlag.Image = "rbxassetid://82688846"
			elseif code == "AF" then
				CountryFlag.Image = "rbxassetid://10780313262"
			elseif code == "AX" then
				CountryFlag.Image = "rbxassetid://6924391098"
			elseif code == "AL" then
				CountryFlag.Image = "rbxassetid://82690675"
			elseif code == "DZ" then
				CountryFlag.Image = "rbxassetid://6924404634"
			elseif code == "AS" then
				CountryFlag.Image = "rbxassetid://9583261608"
			elseif code == "AD" then
				CountryFlag.Image = "rbxassetid://11002315718"
			elseif code == "AO" then
				CountryFlag.Image = "rbxassetid://7964310035"
			elseif code == "AI" then
				CountryFlag.Image = "rbxassetid://5933252830"
			elseif code == "AQ" then
				CountryFlag.Image = "rbxassetid://754202418"
			elseif code == "AG" then
				CountryFlag.Image = "rbxassetid://8457730273"
			elseif code == "AR" then
				CountryFlag.Image = "rbxassetid://6473879287"
			elseif code == "AM" then
				CountryFlag.Image = "rbxassetid://82206853"
			elseif code == "AW" then
				CountryFlag.Image = "rbxassetid://5577307956"
			elseif code == "AU" then
				CountryFlag.Image = "rbxassetid://11240147545"
			elseif code == "AT" then
				CountryFlag.Image = "rbxassetid://11150746432"
			elseif code == "AZ" then
				CountryFlag.Image = "rbxassetid://1386997103"
			elseif code == "BS" then
				CountryFlag.Image = "rbxassetid://82691369"
			elseif code == "BH" then
				CountryFlag.Image = "rbxassetid://82691389"
			elseif code == "BD" then
				CountryFlag.Image = "rbxassetid://82691427"
			elseif code == "BB" then
				CountryFlag.Image = "rbxassetid://82691455"
			elseif code == "BY" then
				CountryFlag.Image = "rbxassetid://116455774"
			elseif code == "BE" then
				CountryFlag.Image = "rbxassetid://5575222552"
			elseif code == "BZ" then
				CountryFlag.Image = "rbxassetid://8448856638"
			elseif code == "BJ" then
				CountryFlag.Image = "rbxassetid://1386975782"
			elseif code == "BM" then
				CountryFlag.Image = "rbxassetid://1387211575"
			elseif code == "BT" then
				CountryFlag.Image = "rbxassetid://10510329400"
			elseif code == "BR" then
				CountryFlag.Image = "rbxassetid://7750763178"
			elseif code == "BG" then
				CountryFlag.Image = "rbxassetid://5680144313"
			elseif code == "KH" then
				CountryFlag.Image = "rbxassetid://10778433472"
			elseif code == "CL" then
				CountryFlag.Image = "rbxassetid://9033083033"
			elseif code == "CN" then
				CountryFlag.Image = "rbxassetid://9110900644"
			elseif code == "CO" then
				CountryFlag.Image = "rbxassetid://6035329148"
			elseif code == "CG" then
				CountryFlag.Image = "rbxassetid://4450483599"
			elseif code == "CW" then
				CountryFlag.Image = "rbxassetid://8009479655"
			elseif code == "CY" then
				CountryFlag.Image = "rbxassetid://5339301368"
			elseif code == "CZ" then
				CountryFlag.Image = "rbxassetid://10868614622"
			elseif code == "DJ" then
				CountryFlag.Image = "rbxassetid://8439481652"
			elseif code == "DM" then
				CountryFlag.Image = "rbxassetid://4985213898"
			elseif code == "DO" then
				CountryFlag.Image = "rbxassetid://7705114675"
			elseif code == "EC" then
				CountryFlag.Image = "rbxassetid://5897221111"
			elseif code == "EG" then
				CountryFlag.Image = "rbxassetid://2408242658"
			elseif code == "SV" then
				CountryFlag.Image = "rbxassetid://2738109367"
			elseif code == "ET" then
				CountryFlag.Image = "rbxassetid://7828341313"
			elseif code == "FJ" then
				CountryFlag.Image = "rbxassetid://16820948"
			elseif code == "FI" then
				CountryFlag.Image = "rbxassetid://9631217519"
			elseif code == "FR" then
				CountryFlag.Image = "rbxassetid://3226406056"
			elseif code == "GE" then
				CountryFlag.Image = "rbxassetid://82206392"
			elseif code == "DE" then
				CountryFlag.Image = "rbxassetid://10031882093"
			elseif code == "GR" then
				CountryFlag.Image = "rbxassetid://3149186217"
			elseif code == "GL" then
				CountryFlag.Image = "rbxassetid://1027647421"
			elseif code == "HK" then
				CountryFlag.Image = "rbxassetid://116839240"
			elseif code == "HU" then
				CountryFlag.Image = "rbxassetid://10845803274"
			elseif code == "IS" then
				CountryFlag.Image = "rbxassetid://11002334472"
			elseif code == "IN" then
				CountryFlag.Image = "rbxassetid://136163916"
			elseif code == "ID" then
				CountryFlag.Image = "rbxassetid://9505068937"
			elseif code == "IE" then
				CountryFlag.Image = "rbxassetid://10194125708"
			elseif code == "JP" then
				CountryFlag.Image = "rbxassetid://5760720814"
			elseif code == "KR" then
				CountryFlag.Image = "rbxassetid://7199539762"
			elseif code == "LU" then
				CountryFlag.Image = "rbxassetid://11181902554"
			elseif code == "LI" then
				CountryFlag.Image = "rbxassetid://120568041"
			elseif code == "MY" then
				CountryFlag.Image = "rbxassetid://6214340412"
			elseif code == "MX" then
				CountryFlag.Image = "rbxassetid://10760076480"
			elseif code == "MM" then
				CountryFlag.Image = "rbxassetid://11226615898"
			elseif code == "NL" then
				CountryFlag.Image = "rbxassetid://5243374959"
			elseif code == "NZ" then
				CountryFlag.Image = "rbxassetid://8392099305"
			elseif code == "NO" then
				CountryFlag.Image = "rbxassetid://624478595"
			elseif code == "PA" then
				CountryFlag.Image = "rbxassetid://10880142719"
			elseif code == "PH" then
				CountryFlag.Image = "rbxassetid://9418075318"
			elseif code == "PL" then
				CountryFlag.Image = "rbxassetid://9124778742"
			elseif code == "QA" then
				CountryFlag.Image = "rbxassetid://23538968"
			elseif code == "SG" then
				CountryFlag.Image = "rbxassetid://118344191"
			elseif code == "ZA" then
				CountryFlag.Image = "rbxassetid://10802610604"
			elseif code == "CH" then
				CountryFlag.Image = "rbxassetid://6035302756"
			elseif code == "TR" then
				CountryFlag.Image = "rbxassetid://3104073690"
			elseif code == "NP" then
				CountryFlag.Image = "rbxassetid://5661746023"
			end
		end
	end
end)
1 Like

My script is a little different to yours but it works just fine! also i would recommend storing the flags in a module.

local frame = script.Parent.Frame
local scrollingframe = frame.ScrollingFrame
local template = scrollingframe.Temp


template.Visible = false

function clearitems()
	for _,items in pairs(scrollingframe:GetChildren()) do
		if items:IsA("TextButton") and items.Name ~= "Temp" then
			items:Destroy()
		end
	end
end

function FillList()
	clearitems()
	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		if not scrollingframe:FindFirstChild(player.Name) then
			local new = template:Clone()
			new.Name = player.Name
			new.Text = player.Name
			new.Parent = scrollingframe
			new.Visible = true
			new.Activated:Connect(function()
				local Localization = game:GetService("LocalizationService")
				local CountryFlag = script.Parent.Frame.ImageLabel
				local player = game.Players:FindFirstChild(new.Name)
				if not player then return end
				local code = Localization:GetCountryRegionForPlayerAsync(player)

				if code == "DK" then
					CountryFlag.Image = "rbxassetid://105525969"
				elseif code == "US" then
					CountryFlag.Image = "rbxassetid://941761111"
				elseif code == "GB" then
					CountryFlag.Image = "rbxassetid://1064519589"
				elseif code == "CA" then
					CountryFlag.Image = "rbxassetid://82688846"
				elseif code == "AF" then
					CountryFlag.Image = "rbxassetid://10780313262"
				elseif code == "AX" then
					CountryFlag.Image = "rbxassetid://6924391098"
				elseif code == "AL" then
					CountryFlag.Image = "rbxassetid://82690675"
				elseif code == "DZ" then
					CountryFlag.Image = "rbxassetid://6924404634"
				elseif code == "AS" then
					CountryFlag.Image = "rbxassetid://9583261608"
				elseif code == "AD" then
					CountryFlag.Image = "rbxassetid://11002315718"
				elseif code == "AO" then
					CountryFlag.Image = "rbxassetid://7964310035"
				elseif code == "AI" then
					CountryFlag.Image = "rbxassetid://5933252830"
				elseif code == "AQ" then
					CountryFlag.Image = "rbxassetid://754202418"
				elseif code == "AG" then
					CountryFlag.Image = "rbxassetid://8457730273"
				elseif code == "AR" then
					CountryFlag.Image = "rbxassetid://6473879287"
				elseif code == "AM" then
					CountryFlag.Image = "rbxassetid://82206853"
				elseif code == "AW" then
					CountryFlag.Image = "rbxassetid://5577307956"
				elseif code == "AU" then
					CountryFlag.Image = "rbxassetid://11240147545"
				elseif code == "AT" then
					CountryFlag.Image = "rbxassetid://11150746432"
				elseif code == "AZ" then
					CountryFlag.Image = "rbxassetid://1386997103"
				elseif code == "BS" then
					CountryFlag.Image = "rbxassetid://82691369"
				elseif code == "BH" then
					CountryFlag.Image = "rbxassetid://82691389"
				elseif code == "BD" then
					CountryFlag.Image = "rbxassetid://82691427"
				elseif code == "BB" then
					CountryFlag.Image = "rbxassetid://82691455"
				elseif code == "BY" then
					CountryFlag.Image = "rbxassetid://116455774"
				elseif code == "BE" then
					CountryFlag.Image = "rbxassetid://5575222552"
				elseif code == "BZ" then
					CountryFlag.Image = "rbxassetid://8448856638"
				elseif code == "BJ" then
					CountryFlag.Image = "rbxassetid://1386975782"
				elseif code == "BM" then
					CountryFlag.Image = "rbxassetid://1387211575"
				elseif code == "BT" then
					CountryFlag.Image = "rbxassetid://10510329400"
				elseif code == "BR" then
					CountryFlag.Image = "rbxassetid://7750763178"
				elseif code == "BG" then
					CountryFlag.Image = "rbxassetid://5680144313"
				elseif code == "KH" then
					CountryFlag.Image = "rbxassetid://10778433472"
				elseif code == "CL" then
					CountryFlag.Image = "rbxassetid://9033083033"
				elseif code == "CN" then
					CountryFlag.Image = "rbxassetid://9110900644"
				elseif code == "CO" then
					CountryFlag.Image = "rbxassetid://6035329148"
				elseif code == "CG" then
					CountryFlag.Image = "rbxassetid://4450483599"
				elseif code == "CW" then
					CountryFlag.Image = "rbxassetid://8009479655"
				elseif code == "CY" then
					CountryFlag.Image = "rbxassetid://5339301368"
				elseif code == "CZ" then
					CountryFlag.Image = "rbxassetid://10868614622"
				elseif code == "DJ" then
					CountryFlag.Image = "rbxassetid://8439481652"
				elseif code == "DM" then
					CountryFlag.Image = "rbxassetid://4985213898"
				elseif code == "DO" then
					CountryFlag.Image = "rbxassetid://7705114675"
				elseif code == "EC" then
					CountryFlag.Image = "rbxassetid://5897221111"
				elseif code == "EG" then
					CountryFlag.Image = "rbxassetid://2408242658"
				elseif code == "SV" then
					CountryFlag.Image = "rbxassetid://2738109367"
				elseif code == "ET" then
					CountryFlag.Image = "rbxassetid://7828341313"
				elseif code == "FJ" then
					CountryFlag.Image = "rbxassetid://16820948"
				elseif code == "FI" then
					CountryFlag.Image = "rbxassetid://9631217519"
				elseif code == "FR" then
					CountryFlag.Image = "rbxassetid://3226406056"
				elseif code == "GE" then
					CountryFlag.Image = "rbxassetid://82206392"
				elseif code == "DE" then
					CountryFlag.Image = "rbxassetid://10031882093"
				elseif code == "GR" then
					CountryFlag.Image = "rbxassetid://3149186217"
				elseif code == "GL" then
					CountryFlag.Image = "rbxassetid://1027647421"
				elseif code == "HK" then
					CountryFlag.Image = "rbxassetid://116839240"
				elseif code == "HU" then
					CountryFlag.Image = "rbxassetid://10845803274"
				elseif code == "IS" then
					CountryFlag.Image = "rbxassetid://11002334472"
				elseif code == "IN" then
					CountryFlag.Image = "rbxassetid://136163916"
				elseif code == "ID" then
					CountryFlag.Image = "rbxassetid://9505068937"
				elseif code == "IE" then
					CountryFlag.Image = "rbxassetid://10194125708"
				elseif code == "JP" then
					CountryFlag.Image = "rbxassetid://5760720814"
				elseif code == "KR" then
					CountryFlag.Image = "rbxassetid://7199539762"
				elseif code == "LU" then
					CountryFlag.Image = "rbxassetid://11181902554"
				elseif code == "LI" then
					CountryFlag.Image = "rbxassetid://120568041"
				elseif code == "MY" then
					CountryFlag.Image = "rbxassetid://6214340412"
				elseif code == "MX" then
					CountryFlag.Image = "rbxassetid://10760076480"
				elseif code == "MM" then
					CountryFlag.Image = "rbxassetid://11226615898"
				elseif code == "NL" then
					CountryFlag.Image = "rbxassetid://5243374959"
				elseif code == "NZ" then
					CountryFlag.Image = "rbxassetid://8392099305"
				elseif code == "NO" then
					CountryFlag.Image = "rbxassetid://624478595"
				elseif code == "PA" then
					CountryFlag.Image = "rbxassetid://10880142719"
				elseif code == "PH" then
					CountryFlag.Image = "rbxassetid://9418075318"
				elseif code == "PL" then
					CountryFlag.Image = "rbxassetid://9124778742"
				elseif code == "QA" then
					CountryFlag.Image = "rbxassetid://23538968"
				elseif code == "SG" then
					CountryFlag.Image = "rbxassetid://118344191"
				elseif code == "ZA" then
					CountryFlag.Image = "rbxassetid://10802610604"
				elseif code == "CH" then
					CountryFlag.Image = "rbxassetid://6035302756"
				elseif code == "TR" then
					CountryFlag.Image = "rbxassetid://3104073690"
				elseif code == "NP" then
					CountryFlag.Image = "rbxassetid://5661746023"
				end
			end)
		end
	end
end

FillList()

game.Players.PlayerAdded:Connect(FillList)
game.Players.PlayerRemoving:Connect(clearitems)

is it all inside the PlayerList script?

in the script that makes all the buttons.

Best to utilise loops, make an array with all the IDs as values and the countries as Keys, then use a pairs loop

local flags = {
	--// Flags
	ExampleName = "Example ID"
}

local plrCountry = ""
local flagID: string = ""

for countryName: string, countryFlagID: string in pairs(flags) do
	if countryName ~= plrCountry then continue end
	
	flagID = countryFlagID
end

--// Continue 
1 Like

The following library/module seems very useful for what you’re trying to achieve:

I did kinda say that but i said to use a module to store them instead.

1 Like

if only i didnt already help him :pensive:

Sorry I didn’t see I was writing the code in studio lol

1 Like

If you want the file then here you go :slight_smile: .
Localization.rbxm (13.9 KB)

@AndreaEliteITA07 Has this worked for your or not?

nope, it doesnt set the image with the id when i click the button