Datastore only updates leaderboard when I rejoin the game

Hey everyone, I am creating a small minigame with a global leader board that shows the score of the top 10 players.

In my game script, it calls for an update in the player’s score once the player finishes/loses the game.

Below is my datastore script, which is trying to update the leader board every time the game ends, and periodically.

Code below updates data after game ends.

UpdateData.Event:Connect(function(player, score)
	
	local success, savedscore = pcall(function()
		return Highscore:GetAsync(player.UserId)
	end)
	
	if success then
		if savedscore == nil then
			Highscore:SetAsync(player.UserId, score)
		else if savedscore <= score then
				Highscore:SetAsync(player.UserId, score)
			end
		end
	end
end)

Code below updates data periodically:

local function UpdateLeaderBoard()
	
	local success, pages = pcall(function()
		return Highscore:GetSortedAsync(false, 10)
	end)

	if success then

		local entries = pages:GetCurrentPage()

		for rank, entry in pairs(entries) do
			local clonedItem = item:Clone()

			local username = game.Players:GetNameFromUserIdAsync(entry.key)

			if username then
				clonedItem.Name = username
				clonedItem.NameText.Text = username
			end

			clonedItem.ScoreText.Text = entry.value
			clonedItem.RankText.Text = rank
			clonedItem.Parent = playersContainer
		end
	end
end

while true do
	task.wait(3)
	UpdateLeaderBoard()
end

However, the leader board does not update when I get a new high score, it only updates when I re-join the game and periodic update completes. When a new player joins and a new score is supposed to be added, it does not update as well until the player re-joins.

How can I solve this? Appreciate the help.

2 Likes

You should add a while task.wait(60) loop in Data store script, that iterates through every player and saves their data, then you will have to make leaderboard auto refresh this way too

Hi all, I have found a solution for this. It turns out that the issue was not with the datastore, but it was due to the fact that I had to remove the clonedItems and readd them after each update iteration. The updated code is as follows:

local function UpdateLeaderBoard()
	
	if removeBoolean then
		playersContainer:ClearAllChildren()
		removeBoolean = false
	end
	
	local success, pages = pcall(function()
		return Highscore:GetSortedAsync(false, 10)
	end)

	if success then
		
		removeBoolean = true
		local UILayout = Instance.new("UIListLayout")
		UILayout.Parent = playersContainer
		UILayout.SortOrder = Enum.SortOrder.LayoutOrder

		local entries = pages:GetCurrentPage()

		for rank, entry in pairs(entries) do
			local clonedItem = item:Clone()

			local username = game.Players:GetNameFromUserIdAsync(entry.key)

			if username then
				clonedItem.Name = username
				clonedItem.NameText.Text = username
			end

			clonedItem.ScoreText.Text = entry.value
			clonedItem.RankText.Text = rank
			clonedItem.Parent = playersContainer
		end
	end
end

Hopefully this will be of help to those having similar problems.

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