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
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)
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.
local playerKey = "Trayeus" -- this is my Username in key format
local success, val = pcall(function()
return sampleDataStore:RemoveAsync(playerKey)
end)