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.