Please help me doing leaderboard with datastore service

  1. What do you want to achieve? i want to achieve leaderboard

  2. What is the issue? Include screenshots / videos if possible!
    i figured out its line 8 by removing pcall when adding back
    here’s the code:

local DataStoreService 	= game:GetService('DataStoreService')
local WinsLeaderboard = DataStoreService:GetOrderedDataStore('WinsLeaderboard')

local function UpdatLeaderboard()
	local success,errorMessage = pcall(function()
		local Data = WinsLeaderboard:GetSortedAsync(false,5) -- error
		for rank, data in pairs(Data:GetCurrentPage()) do
			local UserName = game.Players:GetNameFromUserIdAsync(tonumber(data.Key))
			local Name = UserName
			local Wins = data.Value
			local IsOnLeaderboard = false
			for i, v in pairs(game.Workspace.leaderboard.SurfaceGui.ScrollingFrame:GetChildren()) do
				if v:IsA('Frame') then
					if v:FindFirstChild('Player').Text == Name then
						IsOnLeaderboard = true
						break
					end
				end
				if Wins and IsOnLeaderboard == false then
					local newLbFrame = game.ReplicatedStorage:WaitForChild('leaderboard_frame'):Clone()
					newLbFrame.Name = tostring(rank)
					newLbFrame.Parent = workspace.leaderboard.SurfaceGui.ScrollingFrame
					newLbFrame.Player.Text = Name
					newLbFrame.Rank.Text = '#'..rank
					newLbFrame.Wins.Text = tostring(Wins)
				end
			end
		end
	end)
	if not success then
		print(errorMessage)
	end
end

while true do
	for _, Player in pairs(game.Players:GetPlayers()) do
		WinsLeaderboard:SetAsync(Player.UserId,Player.leaderstats.Wins.Value)
	end
	for _, frame in pairs(game.Workspace.leaderboard.SurfaceGui.ScrollingFrame:GetChildren()) do
		if frame:IsA('Frame') then
			frame:Destroy()
		end
	end
	
	UpdatLeaderboard()
	print('updated')
	wait(10)
end

here is the screenshot of output error


pre last line of output is the error

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

i tried browsing for the people same as i, but eventually i didnt find anything.
also changing Data:GetCurrentPage() to variable and back

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

can you please write where is my mistake and how to fix it?

1 Like

i solved it, for someone who has the same error try this code:

local DataStoreService 	= game:GetService('DataStoreService')
local WinsLeaderboard = DataStoreService:GetOrderedDataStore('WinsLeaderboard41')

local function UpdateLeaderboard()
	local success,errorMessage = pcall(function()
		local Data = WinsLeaderboard:GetSortedAsync(false,5)
		local WinsPage = Data:GetCurrentPage()
		for Rank, data in ipairs(WinsPage) do
			local UserName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
			local Name = UserName
			local Wins = data.Value
			local plr_num = 1
			for _,player in pairs(game.Players:GetPlayers()) do
				local newlb = game.ReplicatedStorage.leaderboard_frame:Clone()
				newlb.Parent = workspace.leaderboard.SurfaceGui.ScrollingFrame
				newlb.Name = tostring(plr_num)
				newlb.Rank.Text ='#'..tostring(plr_num)
				newlb.Player.Text = player.DisplayName
				newlb.Wins.Text = tostring(player:WaitForChild('leaderstats'):WaitForChild('Wins').Value)
				plr_num += 1
			end
			
			end
	end)
	if not success then
		print(errorMessage)
	end
end

while true do
	for _, Player in pairs(game.Players:GetPlayers()) do
		WinsLeaderboard:SetAsync(Player.UserId,Player.leaderstats.Wins.Value)
	end
	for _, frame in pairs(game.Workspace.leaderboard.SurfaceGui.ScrollingFrame:GetChildren()) do
		if frame:IsA('Frame') then
			frame:Destroy()
		end
	end
	
	UpdateLeaderboard()
	print('updated')
	wait(10)
end

i changed creating frames system and data.Key to data.key (lowercase it)

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