Help with my global leaderboard script resetting placements

Hello community, this is a script of Top Global Leaderboard, the script and everything is fine except that there is a bug and that is that after 60 seconds the same character is put back to the list of tops. What can I do?

Script:

-- [ SETTINGS ] --

local StatsName = "Points" -- Your stats name
local MaxItems = 15 -- Max number of items to be displayed on the leaderboard
local MinValueDisplay = 1 -- Any numbers lower than this will be excluded
local MaxValueDisplay = 10e15 -- (10 ^ 15) Any numbers higher than this will be excluded
local UpdateEvery = 60 -- (in seconds) How often the leaderboard has to update

-- [ END SETTINGS ] --




-- Don't edit if you don't know what you're doing --

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetOrderedDataStore("GlobalLeaderboard_" .. StatsName)
local TopLeaderboard = script.Parent
local ListaJugadores = script.ListaJugadores
local List = TopLeaderboard.List
local ItemsFrame = TopLeaderboard.Scrolling

local function GetItems()
	local Data = DataStore:GetSortedAsync(false, MaxItems, MinValueDisplay, MaxValueDisplay)
	local TopPage = Data:GetCurrentPage()
	
	ItemsFrame.Parent.None.Visible = #TopPage == 0 and true or false
	
	for i, v in ipairs(TopPage) do
		local UserId = v.key
		local Value = v.value
		local Username = "[Not Available]"
		local Color = Color3.fromRGB(38, 50, 56)
		
		local Success, Error = pcall(function()
		 	Username = game.Players:GetNameFromUserIdAsync(UserId)
		end)
		
		if i == 1 then
			Color = Color3.fromRGB(255, 255, 0)
		elseif i == 2 then
			Color = Color3.fromRGB(255, 255, 0)
		elseif i == 3 then
			Color = Color3.fromRGB(255, 255, 0)
		elseif i == 4 then
			Color = Color3.fromRGB(255, 255, 0)
		elseif i == 5 then
			Color = Color3.fromRGB(255, 255, 0)
		end
		
		local Item = ListaJugadores:Clone()
		Item.Name = Username
		Item.LayoutOrder = i
		Item.Puesto.TextColor3 = Color
		Item.Puesto.TextStrokeColor3 = Color
		Item.Puesto.Text = i
		Item.Jugador.Text = Username
		Item.POINTS.Text = Value
		Item.Parent = ItemsFrame
	end
end

while true do
	for i, v in pairs(game.Players:GetPlayers()) do
		local Stats = v.leaderstats:WaitForChild(StatsName).Value
		if Stats then
			pcall(function()
				DataStore:UpdateAsync(v.UserId, function(Value)
					return tonumber(Stats)
				end)
			end)
		end
	end
	
	for i, v in pairs(ItemsFrame:GetChildren()) do
		if v:IsA("ImageLabel") then
			v:Destroy()
		end
	end
	GetItems()
	wait(UpdateEvery)
end

Screenshot_34 Screenshot_35

That is all I need :slight_smile:

You should add a checker on the Content of Scrolling frame.

Like this:

local Scrolling = -- Instance of Scrolling frame (already declared on your example)

if Scrolling:FindFirsChild(Username) then
    print("Currently on the list.")
else
    print("Not on the list, adding right now.")
end
1 Like

So I can put that script? :slight_smile:

Yes, but you are the one who will modify it. (Because If I do that, it will considered as Spoonfeeding)

Sorry.

1 Like

Put this on the else part of the if statement I gave.

1 Like

Make sure to correct it, if it gets an error about the FindFirsChild, correct it to:

if ItemsFrame:FindFirstChild(Username) then
1 Like
-- [ SETTINGS ] --

local StatsName = "Points" -- Your stats name
local MaxItems = 15 -- Max number of items to be displayed on the leaderboard
local MinValueDisplay = 1 -- Any numbers lower than this will be excluded
local MaxValueDisplay = 10e15 -- (10 ^ 15) Any numbers higher than this will be excluded
local UpdateEvery = 60 -- (in seconds) How often the leaderboard has to update

-- [ END SETTINGS ] --




-- Don't edit if you don't know what you're doing --

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetOrderedDataStore("GlobalLeaderboard_" .. StatsName)
local TopLeaderboard = script.Parent
local ListaJugadores = script.ListaJugadores
local List = TopLeaderboard.List
local ItemsFrame = TopLeaderboard.Scrolling

local function GetItems()
	local Data = DataStore:GetSortedAsync(false, MaxItems, MinValueDisplay, MaxValueDisplay)
	local TopPage = Data:GetCurrentPage()
	
	ItemsFrame.Parent.None.Visible = #TopPage == 0 and true or false
	
	for i, v in ipairs(TopPage) do
		local UserId = v.key
		local Value = v.value
		local Username = "[Not Available]"
		local Color = Color3.fromRGB(38, 50, 56)
		
		local Success, Error = pcall(function()
		 	Username = game.Players:GetNameFromUserIdAsync(UserId)
		end)
		
		if i == 1 then
			Color = Color3.fromRGB(255, 255, 0)
		elseif i == 2 then
			Color = Color3.fromRGB(255, 255, 0)
		elseif i == 3 then
			Color = Color3.fromRGB(255, 255, 0)
		elseif i == 4 then
			Color = Color3.fromRGB(255, 255, 0)
		elseif i == 5 then
			Color = Color3.fromRGB(255, 255, 0)
		end
		if ItemsFrame:FindFirstChild(Username) then
			ItemsFrame:FindFirstChild(Username):Destroy()
			print("Currently on the list.")
		else
			print("Not on the list, adding right now.")
		end
		
		local Item = ListaJugadores:Clone()
		Item.Name = Username
		Item.LayoutOrder = i
		Item.Puesto.TextColor3 = Color
		Item.Puesto.TextStrokeColor3 = Color
		Item.Puesto.Text = i
		Item.Jugador.Text = Username
		Item.POINTS.Text = Value
		Item.Parent = ItemsFrame
	end
end

while true do
	for i, v in pairs(game.Players:GetPlayers()) do
		local Stats = v.leaderstats:WaitForChild(StatsName).Value
		if Stats then
			pcall(function()
				DataStore:UpdateAsync(v.UserId, function(Value)
					return tonumber(Stats)
				end)
			end)
		end
	end
	
	for i, v in pairs(ItemsFrame:GetChildren()) do
		if v:IsA("ImageLabel") then
			v:Destroy()
		end
	end
	GetItems()
	wait(UpdateEvery)
end

How about this?

-- [ SETTINGS ] --

local StatsName = "Points" -- Your stats name
local MaxItems = 15 -- Max number of items to be displayed on the leaderboard
local MinValueDisplay = 1 -- Any numbers lower than this will be excluded
local MaxValueDisplay = 10e15 -- (10 ^ 15) Any numbers higher than this will be excluded
local UpdateEvery = 60 -- (in seconds) How often the leaderboard has to update

-- [ END SETTINGS ] --




-- Don't edit if you don't know what you're doing --

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetOrderedDataStore("GlobalLeaderboard_" .. StatsName)
local TopLeaderboard = script.Parent
local ListaJugadores = script.ListaJugadores
local List = TopLeaderboard.List
local ItemsFrame = TopLeaderboard.Scrolling

local function GetItems()
	local Data = DataStore:GetSortedAsync(false, MaxItems, MinValueDisplay, MaxValueDisplay)
	local TopPage = Data:GetCurrentPage()
	
	ItemsFrame.Parent.None.Visible = #TopPage == 0 and true or false
	
	for i, v in ipairs(TopPage) do
		local UserId = v.key
		local Value = v.value
		local Username = "[Not Available]"
		local Color = Color3.fromRGB(38, 50, 56)
		
		local Success, Error = pcall(function()
		 	Username = game.Players:GetNameFromUserIdAsync(UserId)
		end)
		
		if i == 1 then
			Color = Color3.fromRGB(255, 255, 0)
		elseif i == 2 then
			Color = Color3.fromRGB(255, 255, 0)
		elseif i == 3 then
			Color = Color3.fromRGB(255, 255, 0)
		elseif i == 4 then
			Color = Color3.fromRGB(255, 255, 0)
		elseif i == 5 then
			Color = Color3.fromRGB(255, 255, 0)
		end
		if ItemsFrame:FindFirstChild(Username) then
			ItemsFrame:FindFirstChild(Username):Destroy()
			print("Currently on the list.")
		else
			print("Not on the list, adding right now.")
		end
		
		local Item = ListaJugadores:Clone()
		Item.Name = Username
		Item.LayoutOrder = i
		Item.Puesto.TextColor3 = Color
		Item.Puesto.TextStrokeColor3 = Color
		Item.Puesto.Text = i
		Item.Jugador.Text = Username
		Item.POINTS.Text = Value
		Item.Parent = ItemsFrame
	end
end

while true do
	for i, v in pairs(game.Players:GetPlayers()) do
		local Stats = v.leaderstats:WaitForChild(StatsName).Value
		if Stats then
			pcall(function()
				DataStore:UpdateAsync(v.UserId, function(Value)
					return tonumber(Stats)
				end)
			end)
		end
	end
	
	for i, v in pairs(ItemsFrame:GetChildren()) do
		if v:IsA("ImageLabel") then
			v:Destroy()
		end
	end
	GetItems()
	wait(UpdateEvery)
end

its yes works! xd :smiley: , Thanks You :smiley:

I created a new code to make it shorter.

Here:

-- This will check if there's no frame that named same as the Username.
if not ItemsFrame:FindFirstChild(Username) then
    local Item = ListaJugadores:Clone()
    Item.Name = Username
    Item.LayoutOrder = i
    Item.Puesto.TextColor3 = Color
    Item.Puesto.TextStrokeColor3 = Color
    Item.Puesto.Text = i
    Item.Jugador.Text = Username
    Item.POINTS.Text = Value
    Item.Parent = ItemsFrame
end
1 Like