Help with Data Reset

So I am making a data reset button. Problem is, whenever I join back, my data is still there. Any help?

local dss = game:GetService("DataStoreService")
local httpservice = game:GetService("HttpService")
local collectedData = dss:GetDataStore("collected")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local collectedFolder = Instance.new("Folder")
	collectedFolder.Name = "Collected"
	collectedFolder.Parent = player

	local collected = Instance.new("IntValue")
	collected.Name = "Collected"
	collected.Value = 0
	collected.Parent = leaderstats

	local playerUserId = "Player_"..player.UserId

	local data
	local success, errormessage = pcall(function()
		data = collectedData:GetAsync(playerUserId)
	end)

	if success then
		collected.Value = data.amount
		local savedCharacters = httpservice:JSONDecode(data.collectedCharacters)
		for _, v in savedCharacters do
			local str = Instance.new("StringValue")
			str.Name = v
			str.Value = v
			str.Parent = collectedFolder
		end
	end
end)


game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId
	local collectedValue = player.leaderstats.Collected.Value
	local newtable = {}
	for _, v in player.Collected:GetChildren() do
		table.insert(newtable, v.Value)
end
local collectedValues = httpservice:JSONEncode(newtable)

local success, errormessage = pcall(function()
	collectedData:SetAsync(playerUserId, {amount = collectedValue, collectedCharacters = collectedValues})
end)
end)

game:BindToClose(function()
	task.wait(5)
end)

game:GetService("ReplicatedStorage"):WaitForChild("SaveGoober").OnServerEvent:Connect(function(player, character)
	local str = Instance.new("StringValue")
	str.Name = character
	str.Value = character
	str.Parent = player.Collected
end)

--here is where delete data is
game:GetService("ReplicatedStorage"):WaitForChild("ResetData").OnServerEvent:Connect(function(player)
	local playerUserId = "Player_"..player.UserId
	local collectedValue = player.leaderstats.Collected.Value
	local newtable = {}
	for _, v in player.Collected:GetChildren() do
		table.insert(newtable, v.Value)
	end
	local collectedValues = httpservice:JSONEncode(newtable)

	local success, errormessage = pcall(function()
		collectedData:RemoveAsync(playerUserId, {amount = collectedValue, collectedCharacters = collectedValues})
		player:Kick("goodbye data")
	end)
end)
1 Like

For the last block of code (with the ResetData Event), I think you can turn in into a simple function like this:

local function resetPlayerData(player)
	local playerUserId = "Player_"..player.UserId

	local success, errormessage = pcall(function()
		collectedData:RemoveAsync(playerUserId)
	end)

	if success then
		player:Kick("Goodbye data")
	else
		print("Failed to reset data:", errormessage)
	end
end

game:GetService("ReplicatedStorage"):WaitForChild("ResetData").OnServerEvent:Connect(function(player)
	resetPlayerData(player)
end)

1 Like

still didnt seem to reset my data…

can you put a print right before RemoveAsync to see if it runs

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId
	local collectedValue = player.leaderstats.Collected.Value
	local newtable = {}
	for _, v in player.Collected:GetChildren() do
		table.insert(newtable, v.Value)
end
local collectedValues = httpservice:JSONEncode(newtable)
local data = {}
data["amount"] = collectedValue
data["collectedCharacters"] = collectedValues
local success, errormessage = pcall(function()
	collectedData:SetAsync(playerUserId, data)
end)
end)

Can you also replace the Removing event with this

Screenshot 2023-08-19 185311

okay, i replaced it.

character limit

While also using the function coolyoshi has written can you now try if it works

yep, doesnt seem to work.

character limit

any chance being kicked also runs the save function and that resaves the data?

you’re absolutely right i didn’t think of that wow.
Yes he needs to also reset the values inside the leaderstats folder

Oh yeah! I swear I made a print saying if it saved or not.

I just now made one.
Screenshot 2023-08-19 192637

im pretty sure the values get reset already, but we dont know that since it keeps saving after getting kicked out.

you need to do

game:GetService("ReplicatedStorage"):WaitForChild("ResetData").OnServerEvent:Connect(function(player)
local playerUserId = "Player_"..player.UserId
for _, v in player.Collected:GetChildren() do
	v.Value = 0
end
player.leaderstats.Collected.Value = 0
local success, errormessage = pcall(function()
		collectedData:RemoveAsync(playerUserId)
		player:Kick("goodbye data")
	end)
end)

thanks! It works perfectly. I just replaced v.Value = 0 with v:Destroy()

1 Like

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