I want the leaderboard to update every 10 seconds with new values (if any)
The issue is that it gives me an error saying it won’t update. I have no clue what the error/bug is.
I have tried adding error messages to tell me where, tried reading the code but everything looked fine.
Code:
local DataStoreService = game:GetService("DataStoreService")
local DonationsLeaderboard = DataStoreService:GetOrderedDataStore("DonationsLeaderboard")
local function updateLeaderboard()
local success, errorMessage = pcall(function()
local Data = DonationsLeaderboard:GetSortedAsync(false, 5)
local DonationsPage = Data:GetCurrentPage()
for Rank, data in ipairs(DonationsPage) do
local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
local Name = userName
local Donations = data.value
local isOnLeaderboard = false
for i, v in pairs(game.Workspace.Leaderboard.SurfaceGui.ScrollingFrame:GetChildren()) do
if v.Player.Text == Name then
isOnLeaderboard = true
break
end
end
if Donations > 0 and isOnLeaderboard == false then
local newLbFrame = game.ReplicatedStorage:WaitForChild("Sample"):Clone()
newLbFrame.Player.Text = Name
newLbFrame.Donations.Text = Donations
newLbFrame.Rank.Text = "#"..Rank
newLbFrame.Position = UDim2.new(0, 0, newLbFrame.Position.Y.Scale + (.08 * #game.Workspace.Leaderboard.SurfaceGui.ScrollingFrame:GetChildren()), 0)
newLbFrame.Parent = game.Workspace.Leaderboard.SurfaceGui.ScrollingFrame
end
end
end)
if not success then
print(errorMessage)
end
end
while true do
for _, player in pairs(game.Players:GetPlayers()) do
DonationsLeaderboard:SetAsync(player.UserId, player.leaderstats.Donations.Value)
end
for _, frame in pairs(game.Workspace.Leaderboard.SurfaceGui.ScrollingFrame:GetChildren()) do
frame:Destroy()
end
if not updateLeaderboard() then
print("Updating leaderboard error")
else
updateLeaderboard()
print("Updated leaderboard")
end
wait(10)
end
Anybody know?
- I’m using a normal script and it is located in ServerScriptService