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)
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)
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)
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)