So far I’ve tried DataStore:GetOrderedDataStore() to get Pages which I can loop through but it cant seem to sort the datastore cause the values aren’t Numbers but Tables.
23:01:45.634 {} - Server - ResetDataStoresScript:85
23:01:46.818 {} - Server - ResetDataStoresScript:85
23:01:48.084 {} - Server - ResetDataStoresScript:85
23:01:49.267 {} - Server - ResetDataStoresScript:85
I’ve only found one other person who has asked the same question on the DevForums, Here. But that post never got anywhere.
My Code Looks Something Like This:
local DataStoreService = game:GetService("DataStoreService")
local Success, DataStorePages = pcall(function()
return DataStoreService:ListDataStoresAsync()
end)
if not Success then return end
-- Gets Data Store Names:
local DataStoreNames = {}
while task.wait() do
local DataStorePage = DataStorePages:GetCurrentPage()
for _, DataStore in pairs(DataStorePage) do
table.insert(DataStoreNames, DataStore.DataStoreName)
end
if DataStorePages.IsFinished then break else DataStorePages:AdvanceToNextPageAsync() end
end
-- Gets Data Of DataStores:
for _, DataStoreName in ipairs(DataStoreNames) do
local DataStore = DataStoreService:GetOrderedDataStore(DataStoreName)
local DataPages = DataStore:GetSortedAsync(false, 10)
while task.wait(1) do
local DataPage = DataPages:GetCurrentPage()
print(DataPage)
for _, Data in pairs(DataPage) do
print(Data.key, Data.Value)
end
if DataPages.IsFinished then break else DataPages:AdvanceToNextPageAsync() end
end
end
Thanks, It’s Working Quite Good Now.
One thing though. You have to run it multiple times cause of it overloading the DataStores.
How could I fix this?
Code:
local DataStoreService = game:GetService("DataStoreService")
local Success, DataStorePages = pcall(function()
return DataStoreService:ListDataStoresAsync()
end)
if not Success then return end
-- Gets Data Store Names:
local DataStoreNames = {}
while task.wait() do
local DataStorePage = DataStorePages:GetCurrentPage()
for _, DataStore in pairs(DataStorePage) do
table.insert(DataStoreNames, DataStore.DataStoreName)
end
if DataStorePages.IsFinished then break else DataStorePages:AdvanceToNextPageAsync() end
end
-- Gets Data Of DataStores:
for _, DataStoreName in ipairs(DataStoreNames) do
local DataStore = DataStoreService:GetDataStore(DataStoreName)
local Success, Pages = pcall(function()
return DataStore:ListKeysAsync()
end)
if not Success then warn("Timeout") continue end
while task.wait() do
local Items = Pages:GetCurrentPage()
for _, Data in ipairs(Items) do
-- Deletes Data
local Key = Data.KeyName
DataStore:RemoveAsync(Key)
end
if Pages.IsFinished then break else Pages:AdvanceToNextPageAsync() end
end
end
local DataStoreService = game:GetService("DataStoreService")
local Success, DataStorePages = pcall(function()
return DataStoreService:ListDataStoresAsync()
end)
if not Success then return end
-- Gets Data Store Names:
local DataStoreNames = {}
while task.wait() do
local DataStorePage = DataStorePages:GetCurrentPage()
for _, DataStore in pairs(DataStorePage) do
table.insert(DataStoreNames, DataStore.DataStoreName)
end
if DataStorePages.IsFinished then break else DataStorePages:AdvanceToNextPageAsync() end
end
-- Gets Data Of DataStores:
for _, DataStoreName in ipairs(DataStoreNames) do
local DataStore = DataStoreService:GetDataStore(DataStoreName)
local Success, Pages = pcall(function()
return DataStore:ListKeysAsync()
end)
if not Success then warn("Timeout") continue end
while task.wait() do
local Items = Pages:GetCurrentPage()
for _, Data in ipairs(Items) do
-- Deletes Data:
local Key = Data.KeyName
DataStore:RemoveAsync(Key)
task.wait(3)
end
if Pages.IsFinished then break else Pages:AdvanceToNextPageAsync() end
end
end