Hi, my global leaderboard isn’t saving since 1 month, I think something is wrong, however stat name and datastore name are supposed to not be wrong.
Here is the code of the global leaderboard :
-- [ SETTINGS ] --
local statsName = "LevelXP" -- Your stats name
local maxItems = 100 -- Max number of items to be displayed on the leaderboard
local minValueDisplay = 1 -- Any numbers lower than this will be excluded
local maxValueDisplay = 10e15 -- (10 ^ 15) Any numbers higher than this will be excluded
local abbreviateValue = true -- The displayed number gets abbreviated to make it "human readable"
local updateEvery = 400 -- (in seconds) How often the leaderboard has to update
local headingColor = Color3.fromRGB(25, 181, 254) -- The background color of the heading
-- [ END SETTINGS ] --
-- Don't edit if you don't know what you're doing --
local CountryService = require(game.ReplicatedStorage.Modules.CountryFlag)
local LocalizationService = game:GetService("LocalizationService")
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local DataStore = DataStoreService:GetOrderedDataStore("GlobalLeaderboard_" .. statsName)
local Frame = script.Parent.LevelsLeaderboard
local Contents = Frame.Contents
local Template = script.objTemplate
local COLORS = {
Default = Color3.fromRGB(38, 50, 56),
Gold = Color3.fromRGB(255, 215, 0),
Silver = Color3.fromRGB(192, 192, 192),
Bronze = Color3.fromRGB(205, 127, 50)
}
local ABBREVIATIONS = { "K", "M", "B", "T" }
local function toHumanReadableNumber(num)
if num < 1000 then
return tostring(num)
end
local digits = math.floor(math.log10(num)) + 1
local index = math.min(#ABBREVIATIONS, math.floor((digits - 1) / 3))
local front = num / math.pow(10, index * 3)
return string.format("%i%s+", front, ABBREVIATIONS[index])
end
local ds = DataStoreService:GetDataStore("PlayerData")
local playersFlagTable = {}
local function getUserIdFlag(userId)
if playersFlagTable[userId]==nil then
local t = {}
local ok, no = pcall(function()
t = ds:GetAsync("plr-"..userId)
end)
if ok then
if t[7] then
playersFlagTable[userId]=t[7]["Country"]
else
playersFlagTable[userId]="None"
end
end
end
return
end
local function getItems()
local data = DataStore:GetSortedAsync(false, maxItems, minValueDisplay, maxValueDisplay)
local topPage = data:GetCurrentPage()
Contents.Items.Nothing.Visible = #topPage == 0 and true or false
for position, v in ipairs(topPage) do
local userId = v.key
local value = v.value
local username = "[Not Available]"
local color = COLORS.Default
local success, err = pcall(function()
username = Players:GetNameFromUserIdAsync(userId)
end)
if position == 1 then
color = COLORS.Gold
elseif position == 2 then
color = COLORS.Silver
elseif position == 3 then
color = COLORS.Bronze
end
if playersFlagTable[userId]==nil then
local t = {}
local ok, no = pcall(function()
t = ds:GetAsync("plr-"..userId)
end)
if ok then
if t[7] then
playersFlagTable[userId]=t[7]["Country"]
else
playersFlagTable[userId]="None"
end
end
end
local function levelToLevelXp(lvl)
if lvl<0 then
return 0
else
return 50+(50*lvl)*.25
end
end
local function levelXpToLevel(xp)
local count=0
local totalXpNeeded=0
repeat
totalXpNeeded += levelToLevelXp(count)
count+=1
until totalXpNeeded>=xp
if totalXpNeeded==xp then
return count
else
return count-1
end
end
local item = Template:Clone()
item.Name = username
item.LayoutOrder = position
item.Values.Number.TextColor3 = color
item.Values.Number.Text = position
item.Values.Username.Text = username
item.Values.Value.Text = "LVL "..tostring(levelXpToLevel(value))
item.Values.Profile.Image = game.Players:GetUserThumbnailAsync(userId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
if playersFlagTable[userId]~=nil and playersFlagTable[userId]~="None" then
item.Values.Country.Image = CountryService.getCountry(playersFlagTable[userId])["decal"]
elseif playersFlagTable[userId]==nil then
getUserIdFlag()
if playersFlagTable[userId]~=nil and playersFlagTable[userId]~="None" then
item.Values.Country.Image = CountryService.getCountry(playersFlagTable[userId])["decal"]
end
end
item.Parent = Contents.Items
end
end
--script.Parent.Parent.Color = headingColor
--Frame.Heading.ImageColor3 = headingColor
while true do
for _, player in pairs(Players:GetPlayers()) do
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then
warn("Couldn't find leaderstats!")
break
end
local statsValue = leaderstats:FindFirstChild(statsName)
if not statsValue then
warn("Couldn't find " .. statsName .. " in leaderstats!")
break
end
pcall(function()
DataStore:UpdateAsync(player.UserId, function()
return tonumber(statsValue.Value)
end)
end)
end
for _, item in pairs(Contents.Items:GetChildren()) do
if item:IsA("Frame") then
item:Destroy()
end
end
getItems()
wait()
wait(updateEvery)
end
What is important to remember is that the name of the stat is “LevelXP” and the one of the datastore is “PlayerData”