Leaderboard error

So I have a leaderboard working but its just that my name keeps duplicating and I don’t want it to do that I just want my name on the leaderboard once…
Here’s a vid to explain more:

As you saw my name was duplicating…

Here is what the explorer looks like:

Here’s the script now:

local leaderboardDataStore = dataStoreService:GetOrderedDataStore("leaderboardDataStore")

local leaderboadPart = workspace.leaderboards.Leaderboard:WaitForChild("MainPart")
local RefreshNum = 5

script.Parent.Gui:Clone()

local function refresh()
	
	for i, Player in pairs(game.Players:GetChildren()) do
		leaderboardDataStore:SetAsync(Player.UserId, Player.leaderstats.Cash.Value)
	end
	
	local suc, err = pcall(function()
		
		local data = leaderboardDataStore:GetSortedAsync(false, 10)
		local CashPage = data:GetCurrentPage()
		
		for Rank, SavedData in pairs(CashPage) do
			local Username = game.Players:GetNameFromUserIdAsync(tonumber(SavedData.key))
			local Cash = SavedData.value
			
			if Cash then
				local NewSample = script.Parent.Gui:Clone()
				
				NewSample.Visible = true
				NewSample.Parent = leaderboadPart.SurfaceGui.Background
				NewSample.Name = Username
				
				NewSample.Rank.Text = "#"..Rank
				NewSample.Username.Text = Username
				NewSample.Value.Text = Cash
			end
		end		
	end)
	
	if not suc then
		warn(err)
	end
end

while wait() do
	refresh()
	print("Leaderboard has been updated")
	wait(RefreshNum)
end

Check first if they exist on the leaderboard before cloning a new GUI for them?

In the refresh function, add a

leaderboadPart.SurfaceGui.Background:ClearAllChildren()

to clear all the names and fully refresh. Full script:

local leaderboardDataStore = dataStoreService:GetOrderedDataStore("leaderboardDataStore")

local leaderboadPart = workspace.leaderboards.Leaderboard:WaitForChild("MainPart")
local RefreshNum = 5

script.Parent.Gui:Clone()

local function refresh()
	leaderboadPart.SurfaceGui.Background:ClearAllChildren()

	for i, Player in pairs(game.Players:GetChildren()) do
		leaderboardDataStore:SetAsync(Player.UserId, Player.leaderstats.Cash.Value)
	end
	
	local suc, err = pcall(function()
		
		local data = leaderboardDataStore:GetSortedAsync(false, 10)
		local CashPage = data:GetCurrentPage()
		
		for Rank, SavedData in pairs(CashPage) do
			local Username = game.Players:GetNameFromUserIdAsync(tonumber(SavedData.key))
			local Cash = SavedData.value
			
			if Cash then
				local NewSample = script.Parent.Gui:Clone()
				
				NewSample.Visible = true
				NewSample.Parent = leaderboadPart.SurfaceGui.Background
				NewSample.Name = Username
				
				NewSample.Rank.Text = "#"..Rank
				NewSample.Username.Text = Username
				NewSample.Value.Text = Cash
			end
		end		
	end)
	
	if not suc then
		warn(err)
	end
end

while wait() do
	refresh()
	print("Leaderboard has been updated")
	wait(RefreshNum)
end

ok but


Ah, sorry. I didn’t take a look at the picture. Instead of that, use this:

for i,v in pairs(leaderboadPart.SurfaceGui.Background:GetChildren()) do
	if v:IsA("Frame") and v.Name ~= "Gui" then
		v:Destroy()
	end
end


still getting that error

Sorry for the late reply, I had something I needed to do, but make sure that the frame named “Gui” is archivable.

for i, v in pairs(script.Parent:GetChildren) do
     if v:IsA("Frame") then
          v:Destroy()
     end
end

Oops my bad, i forgot to add parenthesis.

for i, v in pairs(script.Parent:GetChildren()) do
     if v:IsA("Frame") then
          v:Destroy()
     end
end

Also put this in the beginning of the refresh function.

:////////////

ALright if I DIdN’T EXplAIn RIght after doing that I got this error:
Gui is not a valid member of Frame “Workspace.leaderboards.Leaderboard.MainPart.SurfaceGui.Background”

And if you want the new script here:

local leaderboardDataStore = dataStoreService:GetOrderedDataStore("leaderboardDataStore")

local leaderboadPart = workspace.leaderboards.Leaderboard:WaitForChild("MainPart")
local RefreshNum = 15

local function refresh()
	
	for i, v in pairs(script.Parent:GetChildren()) do
		if v:IsA("Frame") then
			v:Destroy()
		end
	end
	
	for i, Player in pairs(game.Players:GetChildren()) do
		leaderboardDataStore:SetAsync(Player.UserId, Player.leaderstats.Cash.Value)
	end
	
	local suc, err = pcall(function()
		
		local data = leaderboardDataStore:GetSortedAsync(false, 10)
		local CashPage = data:GetCurrentPage()
		
		for Rank, SavedData in pairs(CashPage) do
			local Username = game.Players:GetNameFromUserIdAsync(tonumber(SavedData.key))
			local Cash = SavedData.value
			
			if Cash then
				local NewSample = script.Parent.Gui:Clone()
				
				NewSample.Visible = true
				NewSample.Parent = leaderboadPart.SurfaceGui.Background
				NewSample.Name = Username
				
				NewSample.Rank.Text = "#"..Rank
				NewSample.Username.Text = Username
				NewSample.Value.Text = Cash
			end
		end		
	end)
	
	if not suc then
		warn(err)
	end
end

while wait() do
	refresh()
	print("Leaderboard has been updated")
	wait(RefreshNum)
end