Global leaderboard datastore issue

I’m trying to make a global leaderboard for the first time for total streaks and total kills of an user. The leaderboard shows only total kills for 0.1 seconds every couple of seconds(don’t know exact amount, could be 15) and doesn’t show streaks, also, it positions frame in the middle instead of having streak on the left and kills on the right.
image

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local total_kills_store = DataStoreService:GetOrderedDataStore("Total kills")
local streak_data = DataStoreService:GetOrderedDataStore("Streak")



-- Memoization: since these results are rarely (if ever) going to change
-- all we have to do is check a cache table for the UserId.
-- If we find the UserId, then we have no work to do! Just return the name (fast).
-- If we don't find the UserId (cache miss), go look it up (takes time).
local cache = {}
--https://developer.roblox.com/en-us/api-reference/function/Players/GetNameFromUserIdAsync
function getUsernameFromUserId(userId)

	-- First, check if the cache contains the name
	if cache[userId] then return cache[userId] end

	-- Second, check if the user is already connected to the server
	local player = Players:GetPlayerByUserId(userId)

	if player then
		cache[userId] = player.Name
		return player.Name
	end 
	-- If all else fails, send a request
	local name
	pcall(function ()
		name = Players:GetNameFromUserIdAsync(userId)
	end)
	cache[userId] = name
	return name
end


local model = script.Parent
local mainFrame = model.Leaderboard.SurfaceGui.MainFrame
local listFrame = mainFrame.ScrollingFrame
local userTemplate = mainFrame.UserTemplate


local function updateLeaderboard1()
	local top20
	local success, errorMessage = pcall(function()
		local pages = total_kills_store:GetSortedAsync(false, 20, 1)
		top20 = pages:GetCurrentPage()
	end)
	
	if success then
		for _, item in pairs(listFrame:GetChildren()) do
			if item:IsA("Frame") then
				item:Destroy()
			end
		end

		for index, entry in ipairs(top20) do
			local userCopy = userTemplate:Clone()
			userCopy.Visible = true
			userCopy.Parent = listFrame
			local idFromKey = tonumber(string.sub(entry.key, 8))
			local nameFromId = getUsernameFromUserId(idFromKey)
			if not nameFromId then
				nameFromId = "failed to load name"
			end
			userCopy:WaitForChild("Username").Text = index..".    "..nameFromId
			userCopy.Name = nameFromId
			userCopy:WaitForChild("Amount").Text = entry.value
			
			model:SetAttribute("LowestAmountOnBoard", entry.value)
		end
	else
		print("Leaderboard update failed: "..errorMessage)
	end

end

local function updateLeaderboard2()
	local top20
	local success, errorMessage = pcall(function()
		local pages = streak_data:GetSortedAsync(false, 20, 1)
		top20 = pages:GetCurrentPage()
	end)

	if success then
		for _, item in pairs(listFrame:GetChildren()) do
			if item:IsA("Frame") then
				item:Destroy()
			end
		end

		for index, entry in ipairs(top20) do
			local userCopy = userTemplate:Clone()
			userCopy.Visible = true
			userCopy.Parent = listFrame
			local idFromKey = tonumber(string.sub(entry.key, 8))
			local nameFromId = getUsernameFromUserId(idFromKey)
			if not nameFromId then
				nameFromId = "failed to load name"
			end
			userCopy.Position = Vector2.new(0.5,0)
			userCopy:WaitForChild("Username").Text = index..".    "..nameFromId
			userCopy.Name = nameFromId
			userCopy:WaitForChild("Amount").Text = entry.value

			model:SetAttribute("LowestAmountOnBoard", entry.value)
		end
	else
		print("Leaderboard update failed: "..errorMessage)
	end

end

-- Updates when the game first loads and then every 120 seconds
while true do
	updateLeaderboard1()
	updateLeaderboard2()
	wait(15)
end

You seem to be using the same frame for both leaderboards, so the second refresh script could be deleting the first ones contents.

1 Like

image

You never set the name, value or index text in your script.

1 Like

I am?

			userCopy:WaitForChild("Username").Text = index..".    "..nameFromId
			userCopy.Name = nameFromId
			userCopy:WaitForChild("Amount").Text = entry.value

Change

userCopy.Position = Vector2.new(0.5,0)

To this:

userCopy.Position = Vector2.new(0,0)
1 Like

It’s still in the middle and theres still no streak frames, but now it shows just 1 frame instead of 20 copies of same one.

Are you sure listFrame is on the left?

1 Like

every 15 seconds it adds another frame, an exact copy of previous one


	if success then
		for _, item in pairs(listFrame:GetChildren()) do
			if item:IsA("Frame") and item.Name == "Streak_Frame" then
				item:Destroy()
			end
		end

		for index, entry in ipairs(top20) do
			local userCopy = userTemplate:Clone()
			userCopy.Visible = true
			userCopy.Name = "Streak_Frame"
			userCopy.Parent = listFrame
			local idFromKey = tonumber(string.sub(entry.key, 8))
			local nameFromId = getUsernameFromUserId(idFromKey)
			if not nameFromId then
				nameFromId = "failed to load name"
			end
			userCopy.Position = Vector2.new(0,0)
			userCopy:WaitForChild("Username").Text = index..".    "..nameFromId
			userCopy.Name = nameFromId
			userCopy:WaitForChild("Amount").Text = entry.value

			model:SetAttribute("LowestAmountOnBoard", entry.value)
		end

But is listFrame positioned at 0,0,0,0 or is it in the centre.

1 Like

the frame’s names are ‘nitezion’ i dont know why, It says position is {0,0}{0,0} but i have UI list layout in scrolling menu

Where is the scrolling frame positioned.

1 Like

I removed UI list layout from it and the top kills frames all went from middle to left, but they should be on the right. the scrolling menu position is {0, 0},{0.11, 0}

Move the scrolling frame position to 0.5,0,0.11,0 and insert a fresh UIListLayout into it.

1 Like

If i move the position like that…
image

Then maybe make the size 0.5,0,0.89,0

1 Like

I removed this part from both update functions because it was setting frames inside scrolling menu to names of players, but now it makes just 1 frame instead of 20 copies of same one, I don’t know if i fixed it or jammed it but thats joy of programming.
heres what 0.5,0,0.89,0 looks like
image

It is getting name from my ID and instead of it being ‘Johnny_D3pp’ it ends up being ‘nitezion’, maybe its getting a banned player’s name? I wouldn’t know.

image
image