How to completely deleted a player data

I’m making a datastore system that completely erases/reset player data when they die. But I’m unsure of how to do it and I cannot any other posts that have this problem solved.

You could remove the player’s ds entry by using RemoveAsync.

1 Like

tried that and that didn’t work

Can you show us the code so we can try finding the problem?

1 Like
game.Players.PlayerAdded:Connect(function(plr)
	local itemsave = DataStore:GetAsync(plr.UserId .. "-items") or {}
	
	plr.CharacterAdded:Connect(function(char)
		char.Humanoid.Died:Connect(function()

		end)
	end)
end)

Maybe this will work? (By the way, I recommend using pcall functions for DataStore related stuff)

local function CharacterAdded(Char)
	if Char then
		local Player = game.Players:GetPlayerFromCharacter(Char)
		local Humanoid = Char:WaitForChild("Humanoid")
		Humanoid.Died:Connect(function()
			DataStore:RemoveAsync(Player.UserId .. "-items")
		end)
	end
end


game.Players.PlayerAdded:Connect(function(plr)
	local itemsave = DataStore:GetAsync(plr.UserId .. "-items") or {}
	plr.CharacterAdded(CharacterAdded)
end)
1 Like

Nothing worked the issue still remains

Are there any errors in the output?

nope no errors beside the way RBXSignal of the function you made but I fixed it.

So is it working now, can you show us the code you fixed

no its not working I just fixed up the error.

So I’m guessing no one is going to help me out.

Just an idea, but rather than removing the Data Store key entirely, could you set the player’s data to the ‘default values’ during the initialization process (when the player first joins)?

I imagine you have a set-up similar to that of Apocalypse Rising, which is the perspective my reply shall take.

Since your ‘default data’ is an empty table, upon the players death set their data to an empty table. Ultimately, I think it depends how you’re managing player data. I’m no expert but this is how I would approach your issue.

There used to be a tutorial , I can’t find the webpage to the tutorial sadly, however, I do happen to have a copy of the code from it. Using the Humanoid.Died event (perhaps), here is roughly how I would implement a ‘data reset’ by using the below ModuleScript (of which I take no credit for).

-- Set up table to return to any script that requires this module script
local PlayerStatManager = {}

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")

-- Table to hold player information for the current session
local sessionData = {}

local AUTOSAVE_INTERVAL = 60

-- Function that other scripts can call to change a player's stats
function PlayerStatManager:ChangeStat(player, statName, value)
	local playerUserId = "Player_" .. player.UserId
	assert(typeof(sessionData[playerUserId][statName]) == typeof(value), "ChangeStat error: types do not match")
	if typeof(sessionData[playerUserId][statName]) == "number" then
		sessionData[playerUserId][statName] = sessionData[playerUserId][statName] + value
	else
		sessionData[playerUserId][statName] = value
	end
end

-- Function to add player to the "sessionData" table
local function setupPlayerData(player)
	local playerUserId = "Player_" .. player.UserId
	local success, data = pcall(function()
		return playerData:GetAsync(playerUserId)
	end)
	if success then
		if data then
			-- Data exists for this player
			sessionData[playerUserId] = data
		else
			-- Data store is working, but no current data for this player
			sessionData[playerUserId] = {Money=0, Experience=0}
		end
	else
		warn("Cannot access data store for player!")
	end
end

-- Function to save player's data
local function savePlayerData(playerUserId)
	if sessionData[playerUserId] then
		local tries = 0	
		local success
		repeat
			tries = tries + 1
			success = pcall(function()
				playerData:SetAsync(playerUserId, sessionData[playerUserId])
			end)
			if not success then wait(1) end
		until tries == 3 or success
		if not success then
			warn("Cannot save data for player!")
		end
	end
end

-- Function to save player data on exit
local function saveOnExit(player)
	local playerUserId = "Player_" .. player.UserId
	savePlayerData(playerUserId)
end


-- Function to periodically save player data
local function autoSave()
	while wait(AUTOSAVE_INTERVAL) do
		for playerUserId, data in pairs(sessionData) do
			savePlayerData(playerUserId)
		end
	end
end

-- Connect "setupPlayerData()" function to "PlayerAdded" event
game.Players.PlayerAdded:Connect(setupPlayerData)

-- Connect "saveOnExit()" function to "PlayerRemoving" event
game.Players.PlayerRemoving:Connect(saveOnExit)

-- Start running "autoSave()" function in the background
spawn(autoSave)

return PlayerStatManager

Using the example given on the Humanoid.Died page, you could have a structure a bit like this:

game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()

			-- Call :ChangeStat() and reset to the default table, specified at setUpPlayerData
			
		end)
	end)
end)

(Obviously your code will be cleaner - I was rushing :P)

Again, I am no expert but I hope this can help you in some way or other.

1 Like

Thanks for helping me out ill definitely try to go work on it to see if I can do that when I get back!

I’m not too experienced with Data Stores myself. However, from my reading, what I think you’re wanting is to just ‘reset’ the data value to its original value (rather than remove the key entirely). If you have any issues, let me know and I’ll see if I can write some cleaner code. It really is dependant upon how you’re handling data though.

If you are trying to reset it why not just set it to 0, so you can just use the .Died event and SetAsync it 0.