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