How do I reset all data stat

But the problem is how would I reset it for all players?

It would reset it when they join (You have to keep code forever there)

Either rename datastore key, like you can add attribute “Version” to the script and just change it whenever you like to, it will reset every data (You can change it back anytime to get the old data back)
Don’t forget to add that version thing to datastore’s key

Like this:

local CurrentResetID = 1
local player = game.Players:GetPlayers()[1]
local DataStore = game.DataStoreService
local key = "Player_"..player.UserId

local success, errorMessage
local tries = 0

local Reset = nil

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

	tries += 1
until success or tries >= 5

if Reset == CurrentResetID then
	-- They have been reset do code as normal
else
	-- They haven't been reset
end
1 Like

Renaming the datastore key would also reset all their stats instead of a specific one.

All players stats are all stored in here:

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

Yes, I know, if you want to reset specific stat then do whatever other said or me

Add a bool to their data and check it

or

Make an ID and check their current reset ID and if it dont match then reset it

You could use the code I put above to reset a certain stat, if they don’t match the current reset ID.

1 Like

I guess I will stick with the renaming stat for now. It doesn’t look clean.

["Points 2a"] = 0,
["Currency 2"] = 0,
1 Like

Also, is there a way to do this with DataStoreEditor?

The easiest way would be just check if their data was resetted but ok (:skull:)

1 Like

I don’t know what is this, sorry (Never saw this anywhere)

No, you can’t do that with DataStoreEditor. You can do it with the reset ID script.

1 Like

I don’t know why but data seems to be returning nil:

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

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

local statName = "Currency"

local function ResetAllStats()
	while true do
		for _, keyInstance in ipairs(pages) do
			local userId = keyInstance.KeyName
			local key = "Player_"..userId

			local success, data = pcall(function()
				return DataStore:GetAsync(key)
			end)
			
			print(success, data)

			if success and data then
				data["PlayerStats"][statName] = 0

				local success, err = pcall(function()
					DataStore:UpdateAsync(key, function(oldData)
						return data
					end)
				end)

				if success then
					print("Successfully resetted "..key)
				else
					warn("Failed to reset "..key..": ", err)
				end
			else
				warn("Failed to load data for user ID "..userId..": "..data)
			end
		end
		
		if page.IsFinished then
			break
		else
			page:AdvanceToNextPageAsync()
		end
		
		task.wait()
	end
end

ResetAllStats()

Is it the correct datastore? I saw you were using PlayerStats in the script and you were using another DataStore earlier.

Yeah, I’m sure:

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

Never mind, I got it to work:

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

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

local statName = "Currency"

local function ResetAllStats()
	while true do
		for _, keyInstance in ipairs(pages) do
			local key = keyInstance.KeyName
			
			local success, data = pcall(function()
				return DataStore:GetAsync(key)
			end)

			if success and data then
				data["PlayerStats"][statName] = 0

				local success, err = pcall(function()
					DataStore:UpdateAsync(key, function(oldData)
						return data
					end)
				end)

				if success then
					print("Successfully resetted "..key)
				else
					warn("Failed to reset "..key..": ", err)
				end
			else
				warn("Failed to load data for user ID "..key..": ", data)
			end
		end
		
		if page.IsFinished then
			break
		else
			page:AdvanceToNextPageAsync()
		end
		
		task.wait()
	end
end

ResetAllStats()

It still takes quite a long time. Like maybe 100 every 30 seconds.

If it takes too long then you could try the other script I made, It all counts on how big your player base is.

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