Reset DataStores

Hello. Simply, I need to reset everybody’s datastore. I have a datastore script which is different from roblox’s datastore script.

local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("Chiiken")

local loaded = {}

game.Players.PlayerAdded:Connect(function(player)
	
	local data = datastore.GetAsync(datastore,player.UserId)
	
	local success, value = pcall(datastore.GetAsync, datastore, player.UserId)
	if success == false then player:Kick("DataStore Failed To load, rejoin. If error presits, idk how to fix it lol") return end
	local data = value or {}
	print("loaded: ",data)
	
	for i, folder in game.ServerStorage.PlayerData:GetChildren() do
		local Subdata = data[folder.Name] or {}
		local clone = folder:Clone()
		for i,child in clone:GetChildren() do
			child.Value = Subdata[child.Name] or child.Value
		end
		clone.Parent = player
	end
		loaded[player] = true
end)

game.Players.PlayerRemoving:Connect(function(player)
	if loaded[player] == nil then return end
	local data = {}
	for i, folder in game.ServerStorage.PlayerData:GetChildren() do
		local Subdata = {}
		for i,child in player[folder.Name]:GetChildren() do
			Subdata[child.Name] = child.Value
		end
		data[folder.Name] = Subdata
		player[folder.Name]:Destroy()
	end
	local success, value = pcall(datastore.SetAsync, datastore, player.UserId, data)
	print("saved: ",data)
	loaded[player] = nil
end)


game:BindToClose(function()
	while next(loaded) ~= nil do
		task.wait()
	end
end)

If you want to completely reset everyone’s data, the easiest way to go about it would be to simply change the name of your DataStore; however, be aware that this will count towards your DataStore limit.

3 Likes

Do you want it where when you start a server it’ll loop through and entire datastore and delete each key?

This wouldnt be a good idea as it is always best to limit the amount of data atores you have, you can probably loop through the Keys within the DataStores and use RemoveAsync which removes the data but keeps the key in a accessible state.

Correct, You are limited to 10 - 15 DataStores. And yes you can loop through a datastore and RemoveAsync() on each key

Here is another forum that may help Forum

1 Like

This is a complex problem to solve. You shouldn’t rename the store because it will still count towards the data cap. This code snippet might help:

function clearData(datastore)

	local listSuccess, pages = pcall(function()
		return datastore:ListKeysAsync()
	end)
	if listSuccess then
		local keys = {}
		local pagenum = 0
		while true do
			--print(pagenum)
			local items = pages:GetCurrentPage()

			for key, info in pairs(items) do
				table.insert(keys, info.KeyName)
			end
			if pages.IsFinished then break end
			local s,e = pcall(pages.AdvanceToNextPageAsync, pages)
			--if not s then warn("Deletion pages stopped, moving on.") break end
			if s then
				pagenum+=1
			else
				print("An error occurred, abort!")
				break
			end
			task.wait(1)
		end
		
		for _, key in pairs(keys) do
			local s, verpages = pcall(function()
				--print("Getting versions")
				return datastore:ListVersionsAsync(key, nil, nil, DateTime.now().UnixTimestampMillis - 2592000000)
			end)
			if s then
				while true do
					local items = verpages:GetCurrentPage()

					for dkey, info in pairs(items) do
						--local cts = tostring(info.CreatedTime)
						--local realCreationTime = tonumber(cts:sub(1, cts:len()-3))
						local realCreationTime = info.CreatedTime / 1000
						--print("Key:", dkey, "; Version:", info.Version, "; Created:", os.date("%c", realCreationTime), "; Deleted:", info.IsDeleted)
						if not info.IsDeleted and realCreationTime < os.time()-2592000 then
							print(key, datastore:GetAsync(key))
							warn("The key above will be deleted.")
							datastore:RemoveVersionAsync(key, info.Version)
						end
					end
					if verpages.IsFinished then break end
					verpages:AdvanceToNextPageAsync()
				end
				task.wait(2)
			else
				--print(verpages)
				if verpages:lower():match("later") then
					print("Trying again in 15 seconds")
					task.wait(15)
				end
			end
		end
	else
		print(pages)
	end
end

This is my hacky way of deleting all the keys in a data store. Simply run this function with the datastore as the parameter. Please note that it is currently impossible to delete the metadata of the store, but it will not count towards your data limit (as far as I know).

This will take a few minutes to several hours (maybe even a couple days) depending on how much data you have.

1 Like

Where does it say you can’t have more than 15 datastores…

Limits has no information on this. Data Stores | Roblox Creator Documentation

I have only been able to access/create 10 datastores. Id think Roblox wouldnt just allow you to have hundreds of datastores in one game.

Any evidence to back this up? I’d like to see some error messages here…

Last time I checked it just wouldnt create the datastore under the game. Itd ignore it and do nothing. Let me go recreate it.

Well, Maybe I was wrong or I was doing something differently.
I can’t seem to re create it as the last time I checked was months ago so I cant re-create it effectively

Did not mean to spread false-information.

1 Like

No worries, just like to confirm things like that. As it is potentially extremely useful information.

1 Like

Hello. I no longer need this post as I just did not care what players would do. Thank you for all the help though.

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