How do I reset all data stat

I am looking for a way to reset all players stats so that I can be able to reset their stat to 0.

I don’t want to just change the datastore name because it seems inefficient and I would have to rename it which I don’t like.

Example:

local module = {}
module.PlayerData = {}

function module.CreatePlayerData()
	local data = {
		["Stat"] = {
			["Currency"] = 0,
		},
	}
	
	return data
end

return module

I store it here:

function module.Save(player, DataStore)
	local key = "Player_"..player.UserId
	local data = module.playerDataCache[player.UserId]

	local success, errorMessage
	local tries = 0

	repeat
		success, errorMessage = pcall(function()
			DataStore:SetAsync(key, data)
		end)

		tries += 1
	until success or tries >= 5

	if not success then
		warn("Failed to save player data for ".. player.Name.. ": ".. errorMessage)
	else
		--print("Data saved for "..player.Name)
	end

	return success
end

i made this reply a while ago, you shpouldve been able to find it i think

Just set their coins to 0 when they join the server, create a new stat that stores if they have been reset yet, if yes then don’t touch their coins, if no then set coins to 0 and kick them from the server.

But what if you have like millions of keys in a datastore? Wouldn’t that take forever?

nope not at all its very fast :smiley:

I did something similar but it goes slow:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("PlayerDataStore")

local page = DataStore:ListKeysAsync()
local pages = page:GetCurrentPage()

local currency = "Currency"

for _, info in pages do
	local success, err = pcall(function()
		DataStore:SetAsync(currency, 0)
	end)

	if success then
		print("Successfully reset ", info.KeyName)
	else
		warn("Failed to reset: ", err)
	end

	task.wait()
end

hmmm, i would try the script i gave you, just keep an eye on the output, when i used it for my game it took a few seconds, if you have a million players of keys then I couldnt see it taking more than a few minutes. how about you run it for a bit and see, however if there is so many keys, you may lag from the outputs

But your script resets every datastore in the game, right?

yes it does, if you need it to delete a specific data store im sure i can come up with something

You could also go for this approach, where you could check if they have been reset or not. Make sure you do this as the is loading!

function module.Save(player, DataStore)
	local key = "Player_"..player.UserId
	local data = module.playerDataCache[player.UserId]

	local success, errorMessage
	local tries = 0
	
	local Reset = false
	
	repeat
		success, errorMessage = pcall(function()
			Reset = DataStore:GetAsync("Reset"..key) or false
		end)

		tries += 1
	until success or tries >= 5
	
	if Reset == true then
		-- They have been reset do code as normal
	else
		-- They haven't been reset
	end

	return success
end

you could take this approach if you would like, so that the players that are inactive arent using up resources. however, i would run this as the data is being loaded in, so that the effects are immediate. then you can shut down the servers so that everyone has to go through the reset

But even with this, how would I reset their “Reset” stat back to true when I want to reset their data again?

KEEP in mind, if you do this, the code is going to have to stay there forever, otherwise, players that havenet joined the game when the code was in place, would not be reset

As other person said
Easiest will be just make like a bool value and name it something like “ResettedData” and check if its false, if it is, then reset coins and set “ResettedData” to true

But this is only one time use.

Change name of bool value then
(I don’t understand if you need to reset it when they do something or when they join)

if you havent yet, i would give my script a chance, just shut down the servers, add it into server script service, test out the game and watch the output, there isnt any harm in doing so. just make sure your internet wont cut out halfway through :joy:

I just want to reset all players specific stat after an event ends.

You could make an ID with each time you reset, for example when you reset this time the ID is 123 and the next time is 321. So you could use an ID, rather then a Bool. Then see when the player was last reset and see if it matches with the current reset value.

Then make it check if they have data resetted after event, if not then reset it?