Hello, I’m trying to make a leaderboard in my game, however GetOrderedDataStore() keeps forgetting who certain people are.
here’s my code:
while true do
task.wait(1)
local DetectedPages = DetectedLeaderboard:GetSortedAsync(false, 4, 1)
for rank,v in pairs(DetectedPages:GetCurrentPage()) do
local things = gui.B.ScrollingFrame[rank] -- the leaderboard gui
things.username.Text = game.Players:GetNameFromUserIdAsync(v.key) -- display name
things.ui.Image = "rbxthumb://type=AvatarHeadShot&id=" .. v.key .. "&w=420&h=420"
end
task.wait(20)
end
section where I set the stat:
-- I have a debounce for every player but I don't feel like showing it.
local remote = game.ReplicatedStorage.RequestInformation
local TopDetectedLeaderboardService = dss:GetOrderedDataStore("LeaderboardTopDetected")
remote.OnServerEvent:Connect(function(UserId)
task.wait(20)
local DetectedPages = TopDetectedLeaderboardService:GetSortedAsync(false, 4, 1)
local thispersonisntalready = false
for i,v in pairs(DetectedPages:GetCurrentPage()) do
if tostring(v.key) == tostring(UserId) then
print("set to true")
thispersonisntalready = true
end
end
if thispersonisntalready == false then
for rank, info in pairs(DetectedPages:GetCurrentPage()) do
if info.value <= math.floor(SuspectPointsTotal) then
game.ReplicatedStorage.Dono:FireAllClients(plr.Name .. " just scanned " .. game.Players:GetNameFromUserIdAsync(UserId) .. " who was the top #" .. rank .. " person in the " .. Detector .. " category!", Color3.new(1, 0.258824, 0.270588) )
break
end
end
end
TopDetectedLeaderboardService:SetAsync(UserId, math.floor(SuspectPointsTotal))
end)
This is accurate. I actually benchmarked all of this on another thread some time ago, when generalized iteration was first released.
pairs was fastest, followed by ipairs; generalized iteration proved slower than both, and so did next. However, using any of these iteration methods was still considerably faster than setting up a number-range loop and indexing everything manually. Historically, this wasn’t always the case however. It was only due to optimizations over the years that pairs was made quicker than ipairs, in cases where ipairs would have probably been traditionally preferred. (For context, ipairs only iterates over the strictly array portion of the table, whereas pairs iterates over everything. As such, it was once common practice to use ipairs for arrays, and pairs exclusively for maps).
None of this pertains to the OP’s inquiry, and even if it did, Memezy is also correct in that this is all hardly worth mentioning in the first place, as such optimizations are quite micro. This is such to the extent that the Luau website even advocates that neither ipairsorpairs should be preferred over generalized iteration, as the performance difference is fairly negligable, and generalized iteration, despite technically being slower, still presents a good standardization for most usecases.
that’s cool but why is this bug happening in my leaderboard? Everyone’s just talking about the difference of pairs vs ipairs but nothing about my issue.