Setting a value?

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)

I think you should useipairs() instead of pairs() !
I hope that was helpfull !

will it stop data from being lost like it is currently?

1 Like

Can I see the entire code please then I’ll be able to answer you !

I just edited the post to have it. The code is formatted like this:

  1. Check if they are already top position for that leaderboard ← only used to announce to server
  2. If they are then announce to the server they are
  3. Set the data ← happens regardless of wether they are already top user or not
  • There is a debounce for each player and the expected time the remote event will last is 10-20 seconds
  • Server size is 6 players

do you not know? nobody has responded

the 4 and 1 maybe part of the issue and depending on where the SuspectPointsTotal is being declared or set can be another issue on data lose

you should show all the script as above for help

umm actually someone i know ran benchmarks and pairs is faster by a few nanoseconds :nerd_face: :nerd_face:

It doesnt matter whether they use next, ipairs, pairs, or nothing at all, they all work the same and are only nanoseconds in speed apart

2 Likes

I think your script only show the people stat that are in the game no ?

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 ipairs or pairs 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.

1 Like

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.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.