I want to do a leadboard with the number of the visits of the players from each country

  1. What do you want to achieve? Basically i need to make a leadbaord that show where are the visitators of my game from.

image

I made a script and it works, it adds 1 when a player join from a different country, but my problem is the datasave; in fact i don’t really know how to do it (i mean save a value in a table globally) and i tried to search online for something, but it doesn’t work. Here is my script

local DataStoreService = game:GetService("DataStoreService")
local VISITS_DATASTORE_KEY = "CountryVisits"

local Region = require(game:GetService("ServerScriptService").Region) 

local surfaceGui = script.Parent.SurfaceGui
local scrollingFrame = surfaceGui.Frame.Main.ScrollingFrame
local template = script.Parent.Template

local countryVisits = {} 

local dataStore = DataStoreService:GetDataStore(VISITS_DATASTORE_KEY)

local function incrementVisits(country)
	if not countryVisits[country] then
		countryVisits[country] = 0
	end
	countryVisits[country] += 1
end

local function updateTemplate(template, country)
	local emojiLabel = template.Emoji
	emojiLabel.Text = Region[country].Emoji

	local nameLabel = template.CountryName
	nameLabel.Text = Region[country].Name

	local visitsLabel = template.Amount
	visitsLabel.Text = countryVisits[country] or 0
end

local function getPlayerCountry(player)
	local countryCode = game:GetService("LocalizationService"):GetCountryRegionForPlayerAsync(player)
	return countryCode
end

local function loadVisitCounts()
	local success, data = pcall(function()
		return dataStore:GetAsync(VISITS_DATASTORE_KEY)
	end)
	if success and data then
		countryVisits = data
	end
end

local function saveVisitCounts()
	local success, _ = pcall(function()
		dataStore:SetAsync(VISITS_DATASTORE_KEY, countryVisits)
	end)
	if not success then
		warn("Failed to save visit counts to DataStore")
	end
end

local function updateLeaderboard()
	for i,v in pairs(scrollingFrame:GetChildren()) do
		if not v:IsA("UIListLayout") then
			v:Destroy()
		end
	end
	
	local countryData = {}
	for country, data in pairs(Region) do
		local visitCount = countryVisits[country] or 0
		table.insert(countryData, {Country = country, VisitCount = visitCount})
	end

	table.sort(countryData, function(a, b)
		return a.VisitCount > b.VisitCount
	end)

	for i, countryEntry in ipairs(countryData) do
		local newTemplate = template:Clone()
		newTemplate.Parent = scrollingFrame
		newTemplate.Name = Region[countryEntry.Country].Name
		updateTemplate(newTemplate, countryEntry.Country)
		newTemplate.LayoutOrder = i
	end
end

game.Players.PlayerAdded:Connect(function(player)
	local country = getPlayerCountry(player)
	incrementVisits(country)
	saveVisitCounts()
	updateLeaderboard()
end)

loadVisitCounts()
updateLeaderboard()

while true do
	wait(60)
	saveVisitCounts()
	updateLeaderboard()
end

The “Region” module script is a long table that show the country and its name and emoji
(Example)

(this is the location of every child)
image

ask me for any questions : D

A bit late but u might just want to use this to set up the countries: CountryService (Country data Module)

To save the actual data you could just use a DataStore with a list of countries/visitors