Would this code be suffice to remove player data?

Recently got a message from ROBLOX asking to delete a userID for a right-of-erasure. Would this code, added onto already existing code, be suffice to do this? I am only saving kills and deaths leaderstats in my game.

local DataStoreService = game:GetService("DataStoreService")
local game_Data = DataStoreService:GetDataStore("game_Data")
local erased_Plrs = {
  0000001,
  0000002,
}

--[[
above table would be a simple insert desired id to be erased,
and then would remove id from table at the beginning of every
game/server
--]]

function tablefind(tab,el)
    for index, value in pairs(tab) do
        if value == el then
            return index
        end
    end
end

function CheckTable(tab, val)
    for i,v in ipairs(tab) do
        if v == val then
            return true
        end
    end
    return false
end

for id, v in pairs(erased_Plrs) do
    for id, v in pairs(erased_Plrs) do
    	local success, nickname = pcall(function()
			local k = game_Data:RemoveAsync(id.."-kills")
			local d = game_Data:RemoveAsync(id.."-deaths")
			return k,d
		end)
 
		if success then
			print(id.." stats have been removed.")
		end
                table.remove(erased_Plrs, tablefind(erased_Plrs, id))
    end
end

function onPlayerEntered(newPlayer)
	
	local stats = Instance.new("IntValue")
	stats.Name = "leaderstats"

	local cash = Instance.new("IntValue")
	cash.Name = "Cash" 				
	cash.Value = 25 			

   local kills = Instance.new("IntValue")
	kills.Name = "Kills"
	kills.Value = 0

	local deaths = Instance.new("IntValue")
	deaths.Name = "Deaths"
	deaths.Value = 0
	
	local data1
	local data2
	local success, errormsg = pcall(function()
		data1 = game_Data:GetAsync(newPlayer.UserId.."-kills")
		data2 = game_Data:GetAsync(newPlayer.UserId.."-deaths")
	end)	
	if success then
		kills.Value = data1
		deaths.Value = data2
	else
		print(newPlayer.Name.." stats not loaded!")
		warn(errormsg)	
	end
	
	cash.Parent = stats
	stats.Parent = newPlayer
	kills.Parent = stats
	deaths.Parent = stats

end

game.Players.PlayerRemoving:Connect(function(plr)
	local success, errormsg = pcall(function()
		game_Data:SetAsync(plr.UserId.."-kills",plr.leaderstats.Kills.Value)
		game_Data:SetAsync(plr.UserId.."-deaths",plr.leaderstats.Deaths.Value)
	end)	
	if success then
		print(plr.Name.." stats saved!")
	else
		print(plr.Name.." stats not saved!")
		warn(errormsg)	
	end
end)
4 Likes

Instead of putting the code in your data-handler I believe you can just run this code in the command line

local datastore = game:GetService("DataStoreService"):GetDataStore("RandomDataStore")

datastore:RemoveAsync(key)
2 Likes
local datastore = game:GetService("DataStoreService"):GetDataStore("game_Data")

datastore:RemoveAsync(id_Here.."-kills")
datastore:RemoveAsync(id_Here.."-deaths")

This would be more than enough to run through the command line to delete data?

1 Like

yes, it will set the saved thing to “nil”

1 Like