How do I remove a player from my leaderboard?

Hey, I removed a players data but the players data is still on the leaderboard is there a way to remove that player from the leaderboard aswell?
Or should it already be gone? I tested it on myself and it worked, and what I know I had to rejoin the game and after that it got removed from the leaderboard.

This is the one I used to remove the data. I wrote it in the output.

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("PlayerDataLaunch")
local success,error = pcall(function()
	DS:RemoveAsync("Player_********")
end)
if err then
	console.warn(err)
end
if success then
	print("Removed")
end

Here is the leaderboard script.

local DataStoreService = game:GetService("DataStoreService")
local RankLeaderboard = DataStoreService:GetOrderedDataStore("RankLeaderboard")

local function updateLeaderboard()
	local success, errorMessage = pcall(function()
		local Data = RankLeaderboard:GetSortedAsync(false, 33)
		local RankPage = Data:GetCurrentPage()
		for RankValue, data in ipairs(RankPage) do
			if tonumber(data.key) > 0 then
				local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
			
			local Name = userName
				local Rank = data.value
			local isOnLeaderboard = false
				for i, v in pairs(game.Workspace.GlobalLeaderboardRank.LeaderboardGUI.Holder:GetChildren()) do
				if v.Player.Text == Name then
					isOnLeaderboard = true
					break
				end
			end
		
				if Rank and isOnLeaderboard == false then
					local newLbFrame = game.ReplicatedStorage:WaitForChild("LeaderboardFrameRank"):Clone()
				newLbFrame.Player.Text = Name
					newLbFrame.Rank.Text = Rank
					newLbFrame.RankValue.Text = "#"..RankValue

					if newLbFrame.RankValue.Text == "#1" then
						newLbFrame.RankValue.TextColor3 = Color3.new(0.831373, 0.686275, 0.215686)
						newLbFrame.Player.TextColor3 = Color3.new(0, 0, 0)
						newLbFrame.Rank.TextColor3 = Color3.new(0.831373, 0.686275, 0.215686)
					elseif newLbFrame.RankValue.Text == "#2" then
						newLbFrame.RankValue.TextColor3 = Color3.new(0.752941, 0.752941, 0.752941)
						newLbFrame.Player.TextColor3 = Color3.new(0, 0, 0)
						newLbFrame.Rank.TextColor3 = Color3.new(0.752941, 0.752941, 0.752941)
					elseif newLbFrame.RankValue.Text == "#3" then
						newLbFrame.RankValue.TextColor3 = Color3.new(0.803922, 0.498039, 0.196078)
						newLbFrame.Player.TextColor3 = Color3.new(0, 0, 0)
						newLbFrame.Rank.TextColor3 = Color3.new(0.803922, 0.498039, 0.196078)
					else
						newLbFrame.RankValue.TextColor3 = Color3.new(0.160784, 0.219608, 0.231373)
						newLbFrame.Player.TextColor3 = Color3.new(0, 0, 0)
						newLbFrame.Rank.TextColor3 = Color3.new(0.160784, 0.219608, 0.231373)
					end
					newLbFrame.Position = UDim2.new(0,0, newLbFrame.Position.Y.Scale + (0.03 * #game.Workspace.GlobalLeaderboardRank.LeaderboardGUI.Holder:GetChildren()), 0)
					newLbFrame.Parent = game.Workspace.GlobalLeaderboardRank.LeaderboardGUI.Holder
			end
				
		end
		end
	end)
	
	if not success then
		print(errorMessage)
	end
	
end

while true do
	for _, player in pairs(game.Players:GetPlayers()) do
		RankLeaderboard:SetAsync(player.UserId, player.leaderstats.Rank.Value)

	end
	
	for _, frame in pairs(game.Workspace.GlobalLeaderboardRank.LeaderboardGUI.Holder:GetChildren()) do
		frame:Destroy()
	end
	
	updateLeaderboard()
	print("Updated Rank Leaderboard!")
	
	wait(60)
	
end

If you have any questions please ask me :slight_smile:
Thank you for your help,
/Xsodar

1 Like

I believe SetAsync will only override the stats if your previous stats are higher than what your last SetAsync function called it as

Try changing it to UpdateAsync to override the stat anywhere, regardless of how much you have (You would need to set a function for it)

local DataStoreService = game:GetService("DataStoreService")
local RankLeaderboard = DataStoreService:GetOrderedDataStore("RankLeaderboard")

local function updateLeaderboard()
	local success, errorMessage = pcall(function()
		local Data = RankLeaderboard:GetSortedAsync(false, 33)
		local RankPage = Data:GetCurrentPage()
		for RankValue, data in ipairs(RankPage) do
			if tonumber(data.key) > 0 then
				local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
			
			local Name = userName
				local Rank = data.value
			local isOnLeaderboard = false
				for i, v in pairs(game.Workspace.GlobalLeaderboardRank.LeaderboardGUI.Holder:GetChildren()) do
				if v.Player.Text == Name then
					isOnLeaderboard = true
					break
				end
			end
		
				if Rank and isOnLeaderboard == false then
					local newLbFrame = game.ReplicatedStorage:WaitForChild("LeaderboardFrameRank"):Clone()
				newLbFrame.Player.Text = Name
					newLbFrame.Rank.Text = Rank
					newLbFrame.RankValue.Text = "#"..RankValue

					if newLbFrame.RankValue.Text == "#1" then
						newLbFrame.RankValue.TextColor3 = Color3.new(0.831373, 0.686275, 0.215686)
						newLbFrame.Player.TextColor3 = Color3.new(0, 0, 0)
						newLbFrame.Rank.TextColor3 = Color3.new(0.831373, 0.686275, 0.215686)
					elseif newLbFrame.RankValue.Text == "#2" then
						newLbFrame.RankValue.TextColor3 = Color3.new(0.752941, 0.752941, 0.752941)
						newLbFrame.Player.TextColor3 = Color3.new(0, 0, 0)
						newLbFrame.Rank.TextColor3 = Color3.new(0.752941, 0.752941, 0.752941)
					elseif newLbFrame.RankValue.Text == "#3" then
						newLbFrame.RankValue.TextColor3 = Color3.new(0.803922, 0.498039, 0.196078)
						newLbFrame.Player.TextColor3 = Color3.new(0, 0, 0)
						newLbFrame.Rank.TextColor3 = Color3.new(0.803922, 0.498039, 0.196078)
					else
						newLbFrame.RankValue.TextColor3 = Color3.new(0.160784, 0.219608, 0.231373)
						newLbFrame.Player.TextColor3 = Color3.new(0, 0, 0)
						newLbFrame.Rank.TextColor3 = Color3.new(0.160784, 0.219608, 0.231373)
					end
					newLbFrame.Position = UDim2.new(0,0, newLbFrame.Position.Y.Scale + (0.03 * #game.Workspace.GlobalLeaderboardRank.LeaderboardGUI.Holder:GetChildren()), 0)
					newLbFrame.Parent = game.Workspace.GlobalLeaderboardRank.LeaderboardGUI.Holder
			end
				
		end
		end
	end)
	
	if not success then
		print(errorMessage)
	end
	
end

while true do
	for _, player in pairs(game.Players:GetPlayers()) do
		RankLeaderboard:UpdateAsync(player.UserId, function(OldValue)
            local NewValue = OldValue or 0
            return NewValue
        end)

	end
	
	for _, frame in pairs(game.Workspace.GlobalLeaderboardRank.LeaderboardGUI.Holder:GetChildren()) do
		frame:Destroy()
	end
	
	updateLeaderboard()
	print("Updated Rank Leaderboard!")
	
	wait(60)
	
end

I don’t know if you updated the script in the command bar or not, but you had the wrong data store name and type. It should be OrderedDataStore, as it is for a leaderboard, as well as named “RankLeaderboard” instead of “PlayerDataLaunch.”

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetOrderedDataStore("RankLeaderboard")
local success,error = pcall(function()
	DS:RemoveAsync("Player_********")
end)
if err then
	console.warn(err)
end
if success then
	print("Removed")
end
2 Likes

No, according to the API Reference, SetAsync is described as:
Sets the value of the key. This overwrites any existing data stored in the key…
So it sets the value regardless of previous data. But, what you set about using UpdateAsync was right. SetAsync should never, ever be used to update data, hence the name. It should only be used to set the initial data. Afterwards, to update the data, use UpdateAsync.

1 Like