Delete a user Data from Ordered Data Store?

I have this global leaderboard created with Ordered Data Store that saves data by username that shows how many times they visited, how can I remove someone from it?

Sorry If i don’t fully understand data stores…

Here’s my current script for the ordered Data Store:

local Players = game:GetService(“Players”)
local ods = game:GetService(“DataStoreService”):GetOrderedDataStore(“Visits”)
local clickDetector = Instance.new(“ClickDetector”)
clickDetector.Parent = script.Parent
local canClick = true

function onPlayerAdded(player)
print(player.Name … " has entered the game")

ods:IncrementAsync(player.Name, 1)
end

–When a player joins, call the onPlayerAdded function

Players.PlayerAdded:connect(onPlayerAdded)

–Call onPlayerAdded for each player already in the game
for _,player in pairs(Players:GetPlayers()) do
onPlayerAdded(player)
end

1 Like

You could potentially use SetAsync() and just reset their “visits” to zero, quick word of advice however. When using datastores it is of best practice to use the user ID and not the name, as if a player changes their name, they will lose their data.

Edit (Sorry):
On further notice, there is a function in the ordered datastore just for this! Use RemoveAsync(key)

@brandonbrady1 did @Pyxrien’s code work/solve your issue? If so, make sure to mark it as the solution!

Along with RemoveAsync(key), you can also SetAsync(key, nil).
This should delete the key as well!

the RemoveAsync works on me since i have it erase my data when i join, But i am trying to remove a data for a specific user

Right, in what instance are you using this? Like for debugging or is this going to be used in saving system?

First, like @Pyxrien mentioned,

You can achieve this by using something like the following:

local playerKey = "Player_" .. player.UserId

local success, val = pcall(function()
	return sampleDataStore:RemoveAsync(playerKey)
end)

This is tagging each Player’s DataStore data by their UserId which will never change (not dependent on their username (Player.Name)!

So, if you wanted to remove me from your DataStore you could fire something like:

local playerKey = "Player_13133526" -- this is my UserId in key format

local success, val = pcall(function()
	return sampleDataStore:RemoveAsync(playerKey)
end)

But, this would only work if you were already using the UserId key format.

1 Like

SpecificDataStore:SetAsync(KeyOfThatPlayer, Nil)

So If i have it save by player name I can’t delete?

No, you can. You would do it like:

local playerKey = "Trayeus" -- this is my Username in key format

local success, val = pcall(function()
	return sampleDataStore:RemoveAsync(playerKey)
end)
3 Likes

Dude, Thank you so much!

This is the solution to my problem

1 Like

Awesome! Glad to help! :slight_smile:
Feel free to reach out if you need more help!

1 Like