So I have made a global leaderboard before, but after a few days, it does not update and too many datastores are added to the queue. I was wondering if you could help me refine and improvise my code to make it more efficient and work every time? Thank You For your time!
Here is the code that I have now:
local dataStoreService = game:GetService("DataStoreService")
local players = game:GetService("Players")
local globalDataStore = dataStoreService:GetOrderedDataStore("Wins")
local board = workspace.Lobby.GlobalBoard
local template = board.SurfaceGui.LeaderBoard.Template:Clone()
board.SurfaceGui.LeaderBoard.Template:Destroy()
local function update()
for _,child in pairs(board.SurfaceGui.LeaderBoard:GetChildren()) do
if child:IsA("Frame") then
child:Destroy()
end
end
local success,err = pcall(function()
local data = globalDataStore:GetSortedAsync(false,40)
local page = data:GetCurrentPage()
for rank,plrData in ipairs(page) do
local userid = plrData.key
local wins = plrData.value
end
local new = template:Clone()
new.PlrName.Text = players:GetNameFromUserIdAsync(userid)
new.PlrAmount.Text = wins
new.LayoutOrder = rank
new.Parent = board.SurfaceGui.LeaderBoard
end
end)
end
while true do
update()
wait(math.random(2,18))
spawn(function()
for _,plr in pairs(game.Players:GetPlayers()) do
globalDataStore:SetAsync(plr.UserId,plr.leaderstats.Wins.Value)
wait()
end
end)
end

1 comment
Change the pairs to ipairs for one. That’s all I can really think of