Remove player from ordered data store

I created a global leaderboard that uses an ordered data store to display the top 25 players, the problem I have is that the players sometimes cannot be found.

Is there a way to delete such players from the ordered data store?

Here is the following code for creating a global leaderboard depending on the player’s level:

local dss = game:GetService("DataStoreService")

local data = dss:GetOrderedDataStore("DataTest02")

local storedDataName = "Level"

local event = game:GetService("ReplicatedStorage"):WaitForChild("Leaderboard")

wait(5)

while true do
	for i, player in pairs(game.Players:GetPlayers()) do
		local exp = player:FindFirstChild("Experience")
		if exp then
			local levels = exp.Level
			if levels then
				pcall(function()
					data:UpdateAsync(player.UserId,function(oldVal)
						return levels.Value
					end)
				end)
			end
		end
	end
	
	local pages = data:GetSortedAsync(false, 25)
	local top = pages:GetCurrentPage()
	local datatab = {} 
	local on = 1
	
	for i, data in ipairs(top) do
		local userid = data.key
		local level = data.value
		local name = "Loading..."
		local s, e = pcall(function()
			name = game.Players:GetNameFromUserIdAsync(userid)
		end)
		if not s then
			warn("Erros getting name for ".. userid .. ". Error: ".. e)
		end
		local image = "https://www.roblox.com/headshot-thumbnail/image?userId=" .. userid .. "&width=150&height=150&format=png"
		datatab[on] = {
			["Name"] = name,
			["Image"] = image,
			["Wins"] = level
		}
		on = on + 1
	end
	event:FireAllClients(datatab)
	wait(10)
end
1 Like

This is how it looks on the leaderboard if anyone is wondering

1 Like

you can remove them by calling RemoveAsync on the datastore

local dss = game:GetService("DataStoreService")

local data = dss:GetOrderedDataStore("DataTest02")

local storedDataName = "Level"

local event = game:GetService("ReplicatedStorage"):WaitForChild("Leaderboard")

wait(5)

while true do
	for i, player in pairs(game.Players:GetPlayers()) do
		local exp = player:FindFirstChild("Experience")
		if exp then
			local levels = exp.Level
			if levels then
				pcall(function()
					data:UpdateAsync(player.UserId,function(oldVal)
						return levels.Value
					end)
				end)
			end
		end
	end
	
	local pages = data:GetSortedAsync(false, 25)
	local top = pages:GetCurrentPage()
	local datatab = {} 
	local on = 1
	
	for i, data in ipairs(top) do
		local userid = data.key
		local level = data.value
		local name = "Loading..."
		local s, e = pcall(function()
			name = game.Players:GetNameFromUserIdAsync(userid)
		end)
		if not s then
			warn("Erros getting name for ".. userid .. ". Error: ".. e)
            data:RemoveAsync(data.key)
		end
		local image = "https://www.roblox.com/headshot-thumbnail/image?userId=" .. userid .. "&width=150&height=150&format=png"
		datatab[on] = {
			["Name"] = name,
			["Image"] = image,
			["Wins"] = level
		}
		on = on + 1
	end
	event:FireAllClients(datatab)
	wait(10)
end
2 Likes

RemoveAsync doesnt exist for the table

the datastore variable data is being overwritten by the data variable you are using in the for loop. change the name of the datastore variable

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.