Whenever my global leaderboard refreshes, it repeats.
How do I fix this?
local DataStoreService = game:GetService("DataStoreService")
local StudsDataStore = DataStoreService:GetOrderedDataStore("StudsLeaderboard")
local LeaderboardPart = script.Parent.Parent.Parent
local RefreshRate = 10
local function RefreshLeaderboard()
for i, Player in pairs(game.Players:GetPlayers()) do
StudsDataStore:SetAsync(Player.UserId, Player.leaderstats.Studs.Value)
end
local Suscess, Error = pcall(function()
local Data = StudsDataStore:GetSortedAsync(false, 10)
local StudsPage = Data:GetCurrentPage()
for Rank, SavedData in ipairs(StudsPage) do
local Username = game.Players:GetNameFromUserIdAsync(tonumber(SavedData.key))
local Studs = SavedData.value
if Studs then
local NewSample = script.Parent.Sample:Clone()
NewSample.Visible = true
NewSample.Parent = LeaderboardPart.SurfaceGui.PlayerHolder
NewSample.Name = Username
NewSample.RankLabel.Text = "#"..Rank
NewSample.NameLabel.Text = Username
NewSample.StudsLabel.Text = Studs
end
end
end)
end
while true do
for i, ScrollingFrame in pairs(LeaderboardPart.SurfaceGui.PlayerHolder:GetChildren()) do
if ScrollingFrame.Name ~= "Sample" and ScrollingFrame:IsA("ScrollingFrame") then
ScrollingFrame:Destroy()
end
end
RefreshLeaderboard()
wait(RefreshRate)
end```
