Leaderboard with Country Flags

this is really cool, but how would you get a players country lol

Like:


localizationservice:GetCountryRegionForPlayerAsync(player)

oh i thought that was pseudo code mb

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local LocalizationService = game:GetService("LocalizationService")

local maxRankingsToShow = 100
local rankingList = script.Parent.SurfaceGui.Frame.Background.Items
local statsName = "TimePlayed" --leaderstats
local valueData = DataStoreService:GetOrderedDataStore(statsName.."_V3")
local countryData = DataStoreService:GetDataStore("Country_V3")

local updateTime = 45

local Cache = {}

local function getPlayerName(userId)
	if Cache[userId] and Cache[userId].name then
		return Cache[userId].name
	end
	
	local playerName = Players:GetNameFromUserIdAsync(userId)
	Cache[userId] = {name = playerName}
	return playerName
end

local function setCountryEmoji(country)
	local code = string.upper(country)
	local first = string.byte(string.sub(code, 1, 1)) - 0x41 + 0x1F1E6
	local second = string.byte(string.sub(code, 2, 2)) - 0x41 + 0x1F1E6
	return utf8.char(first) .. utf8.char(second)
end


local function updateRankingList()
	rankingList:ClearAllChildren()

	local success, topTen = pcall(function()
		return valueData:GetSortedAsync(false, maxRankingsToShow):GetCurrentPage()
	end)
	if not success then return end

	rankingList.CanvasSize = UDim2.new(0, 0, 0, #topTen * 65)
	for rank, data in ipairs(topTen) do
		local playerName = getPlayerName(data.key)
		if not Cache[data.key].country then
			local success, countryValue = pcall(function()
				return countryData:GetAsync(data.key)
			end)
			if success then
				Cache[data.key].country = countryValue
			else
				Cache[data.key].country = ""
			end
		end
		local country = Cache[data.key].country
		playerName = playerName or "Unknown"
		local rankingFrame = script.Template:Clone()
		rankingFrame.Username.Text = rank .. ". " .. playerName .. " " .. country
		local values = data.value
		rankingFrame.Value.Text = values < 1000 and tostring(values) or string.format("%d.%03d", math.floor(values/1000), math.fmod(values, 1000))
		rankingFrame.Position = UDim2.new(0,0,0,(rank - 1) * 65)
		rankingFrame.Parent = rankingList
		wait()
	end
end

while true do
	for _, player in pairs(game.Players:GetPlayers()) do
		local leaderstats = player:FindFirstChild("leaderstats")
		if leaderstats then
			local statsValue = leaderstats:FindFirstChild(statsName)
			if statsValue then
				pcall(function()
					valueData:UpdateAsync(player.UserId, function(oldValue)
						return tonumber(statsValue.Value)
					end)
				end)
			end
		end

		pcall(function()
			countryData:UpdateAsync(player.UserId, function(oldCountry)
				local success, country = pcall(function() return setCountryEmoji(LocalizationService:GetCountryRegionForPlayerAsync(player)) end)
				if success then
					return country
				end
				return oldCountry
			end)
		end)
	end

	pcall(function() updateRankingList() end)

	wait(updateTime)
end

updateRankingList()
		

Heres the sourcecode buddy, you can clearly see its safe as it doesnt require any 3rd party modules, use getfenv, etc.

1 Like

This is not true at all… You can easily re-sort the array man and edit the string.

Well, not an expert but as he uses some kind of extreme math that gets the nation and translates it into an emoji, you would have to change basically everything, as you need to put like:
Brazil = :+1:
Canada = :rofl:
Etc. Etc.

That’s not extreme math. It’s not math at all


local function setCountryEmoji(country) -- country input example: us
	local code = string.upper(country) -- US
	local first = string.byte(string.sub(code, 1, 1)) - 0x41 + 0x1F1E6 -- unicode character for U
	local second = string.byte(string.sub(code, 2, 2)) - 0x41 + 0x1F1E6 -- unicode character for S
	return utf8.char(first) .. utf8.char(second) -- US unicode characters put together becomes 🇺🇸 which will be displayed as the flag.
end

Well, there’s still math, but why i meant is that he’s doing some complex mechanisms