Roblox does no allow mixed tables to be saved into the datastore
datastore.Value = {
[1] = "A",
[2] = "B",
[3] = "C",
["Index1"] = "D", -- this will not be saved
["Index2"] = "E", -- this will not be saved
}
-- everything will not save correctly because this is a array + dictionary
-- only the array part will save
datastore.Value = {
["1"] = "A",
["2"] = "B",
["3"] = "C",
["Index1"] = "D",
["Index2"] = "E",
}
-- everything will now save correctly because this is only a dictionary
-- there is no array part
Roblox will automatically save a backup for 30 days you can read about it here Data Stores | Documentation - Roblox Creator Hub
to access this you can do
local DataStoreService = game:GetService("DataStoreService")
local playerStore = DataStoreService:GetDataStore("Player")
local success, pages = pcall(playerStore.ListVersionsAsync, playerStore, player.UserId)
if success == false then error("Failed to get versions") end
local items = pages:GetCurrentPage()
for key, info in items do
print("Key:", key, "; Version:", info.Version, "; Created:", info.CreatedTime, "; Deleted:", info.IsDeleted)
end
but if if you want to save your own version then you can do this
local DataStoreModule = require(11671168253)
local DataStoreService = game:GetService("DataStoreService")
local template = {
Level = 0,
Coins = 0,
Inventory = {},
Version = 0,
}
local function StateChanged(state, dataStore)
while dataStore.State == false do
if dataStore:Open(template) ~= "Success" then task.wait(6) end
end
end
game.Players.PlayerAdded:Connect(function(player)
local dataStore = DataStoreModule.new("Player", player.UserId)
dataStore.StateChanged:Connect(StateChanged)
StateChanged(dataStore.State, dataStore)
end)
game.Players.PlayerRemoving:Connect(function(player)
local dataStore = DataStoreModule.find("Player", player.UserId)
if dataStore == nil then return end -- when the server starts to shutdown it might destroy the datastore so a backup might not be saved for the players online while the server shuts down
dataStore.Value.Version += 1
dataStore:Destroy()
local playerBackupStore = DataStoreService:GetDataStore("PlayerBackup", dataStore.Key)
local success = pcall(playerBackupStore.SetAsync, playerBackupStore, dataStore.Value.Version, dataStore.Value)
end)
here is another version that is a bit more safer
local DataStoreModule = require(11671168253)
local DataStoreService = game:GetService("DataStoreService")
local template = {
Level = 0,
Coins = 0,
Inventory = {},
Version = 0,
}
local function StateChanged(state, dataStore)
while dataStore.State == false do
if dataStore:Open(template) ~= "Success" then task.wait(6) end
end
end
local function Saving(value, dataStore)
value.Version += 1
local playerBackupStore = DataStoreService:GetDataStore("PlayerBackup", dataStore.Key)
local success = pcall(playerBackupStore.SetAsync, playerBackupStore, value.Version, value)
end
game.Players.PlayerAdded:Connect(function(player)
local dataStore = DataStoreModule.new("Player", player.UserId)
dataStore.StateChanged:Connect(StateChanged)
dataStore.Saving:Connect(Saving)
StateChanged(dataStore.State, dataStore)
end)
game.Players.PlayerRemoving:Connect(function(player)
local dataStore = DataStoreModule.find("Player", player.UserId)
if dataStore ~= nil then dataStore:Destroy() end
end)