I want to make global leaderboard. Problem is that global leaderboard doesn’t work. I tried to use datastores and watched tutorials but those doesn’t help my code. I asked ChatGpt but even ChatGpt doesn’t have any solutions. My server leaderboard works normally so I think that I can use server leaderboard code and put datastore and use datastore but I am not good at put data into datastore and use it to make working global leaderboard.
Here is code:
local ds = game:GetService("DataStoreService")
local data = ds:GetOrderedDataStore("XXXXX")
local glpart = script.Parent.Parent.Parent
local CTRLR = 10
local function formatNumber(n)
if n >= 1e66 then
return string.format("%.1f", n / 1e66):gsub("%.?0+$", "") .. "Uvg"
elseif n >= 1e63 then
return string.format("%.2f", n / 1e63):gsub("%.?0+$", "") .. "Vg"
elseif n >= 1e60 then
return string.format("%.2f", n / 1e60):gsub("%.?0+$", "") .. "Nod"
elseif n >= 1e57 then
return string.format("%.2f", n / 1e57):gsub("%.?0+$", "") .. "Ocd"
elseif n >= 1e54 then
return string.format("%.2f", n / 1e54):gsub("%.?0+$", "") .. "Spd"
elseif n >= 1e51 then
return string.format("%.2f", n / 1e51):gsub("%.?0+$", "") .. "Sxd"
elseif n >= 1e48 then
return string.format("%.2f", n / 1e48):gsub("%.?0+$", "") .. "Qid"
elseif n >= 1e45 then
return string.format("%.2f", n / 1e45):gsub("%.?0+$", "") .. "Qad"
elseif n >= 1e42 then
return string.format("%.2f", n / 1e42):gsub("%.?0+$", "") .. "Td"
elseif n >= 1e39 then
return string.format("%.2f", n / 1e39):gsub("%.?0+$", "") .. "Dd"
elseif n >= 1e36 then
return string.format("%.2f", n / 1e36):gsub("%.?0+$", "") .. "Ud"
elseif n >= 1e33 then
return string.format("%.2f", n / 1e33):gsub("%.?0+$", "") .. "Dc"
elseif n >= 1e30 then
return string.format("%.2f", n / 1e30):gsub("%.?0+$", "") .. "no"
elseif n >= 1e27 then
return string.format("%.2f", n / 1e27):gsub("%.?0+$", "") .. "Oc"
elseif n >= 1e24 then
return string.format("%.2f", n / 1e24):gsub("%.?0+$", "") .. "Sp"
elseif n >= 1e21 then
return string.format("%.2f", n / 1e21):gsub("%.?0+$", "") .. "Sx"
elseif n >= 1e18 then
return string.format("%.2f", n / 1e18):gsub("%.?0+$", "") .. "Gi"
elseif n >= 1e15 then
return string.format("%.2f", n / 1e15):gsub("%.?0+$", "") .. "Qa"
elseif n >= 1e12 then
return string.format("%.2f", n / 1e12):gsub("%.?0+$", "") .. "T"
elseif n >= 1e9 then
return string.format("%.2f", n / 1e9):gsub("%.?0+$", "") .. "B"
elseif n >= 1e6 then
return string.format("%.2f", n / 1e6):gsub("%.?0+$", "") .. "M"
elseif n >= 1e3 then
return string.format("%.2f", n / 1e3):gsub("%.?0+$", "") .. "K"
else
return tostring(n)
end
end
local function refreshLeaderboard()
local players = game.Players:GetPlayers()
local leaderboardData = {}
for _, player in pairs(players) do
local gems = player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Gems")
if gems then
leaderboardData[player.UserId] = formatNumber(gems.Value) -- Käytetään formatNumber-funktiota tässä
end
end
local success, error = pcall(function()
local orderedLeaderboard = {}
for userId, gemValue in pairs(leaderboardData) do
table.insert(orderedLeaderboard, { key = userId, value = gemValue })
end
table.sort(orderedLeaderboard, function(a, b)
return a.value > b.value
end)
for rank, entry in ipairs(orderedLeaderboard) do
local username = game.Players:GetNameFromUserIdAsync(entry.key)
local gem = entry.value
local nsapmle = script.Parent.Sample:Clone()
nsapmle.Visible = true
nsapmle.Parent = glpart.SurfaceGui.plrhandler
nsapmle.Name = username
nsapmle.prank.Text = "#"..rank
nsapmle.pname.Text = username
nsapmle.pgems.Text = gem
end
end)
if not success then
warn("Error updating leaderboard:", error)
end
end
while true do
for _, frame in pairs(glpart.SurfaceGui.plrhandler:GetChildren()) do
if frame.Name ~= "Sample" and frame:IsA("Frame") then
frame:Destroy()
end
end
refreshLeaderboard()
wait(CTRLR)
end
more info:
“local glpart = script.Parent.Parent.Parent = main (part)”
“local data = ds:GetOrderedDataStore(“XXXXX”)” We can use the datastore name as “XXXXX”
PATH:
ServerStats
-main
-SurfaceGui
-plrhandler (ScrollingFrame)
-GL (script)
-UIListLayout
-Sample (Frame)
-pgems (TextLabel)
-pname (TextLabel)
-prank (TextLabel)
I’m really glad if this can be solved!
Tell me please if I made something wrong in this post. This is my second post…