Hello guys, I recently released my game but I’m getting reports of data loss when players rejoin. I heard DataStore2 is a good method to save player data but I am unsure of how to go from normal datastores to the datastore2 module. Help is appreciated!
I know how to use it, I am just unsure of how to transfer data from normal datastores to datastore2.
There is no way to iterate through a datastore so a good way to migrate would be to change your code to support DataStore2 and then move everyone’s data into a DataStore2 store on PlayerAdded.
you’ll have to see whether a player has data in DataStore2, if not, then get data through the current DataStore and save using DataStore2
Is this the right way to do it?
local Modules = script.Modules
local PlayerData = {};
local Data = {};
local DataStoreService = game:GetService("DataStoreService")
local PlayerSavesDatastore = DataStoreService:GetDataStore("ALPHA_RELEASE") -- original datastore
local DataStore2 = require(Modules:WaitForChild('DataStore2'));
local GetDataValues = require(Modules:WaitForChild('DataValues')); --returns table with default data
--//Player Added
Players.PlayerAdded:Connect(function(P)
local UserId = tostring(P.UserId);
local _, DataStore = pcall(function()
if PlayerSavesDatastore:GetAsync(UserId) ~= nil then
print("Previous data from old data store found, level "..PlayerSavesDatastore:GetAsync(UserId).Level)
return DataStore2("ALPHA_RELEASE",P) --old datastore key
end
end)
PlayerData[UserId] = DataStore;
Data[UserId] = PlayerData[UserId]:GetTable(GetDataValues(UserId))
end)
This is what I have for saving:
--//Save Data Function
local function saveData(Plr,Table)
local UserId = tostring(Plr.UserId);
local DataStore = PlayerData[UserId];
DataStore:Set(Table);
DataStore:Save();
end;
--//Player Removing
Players.PlayerRemoving:Connect(function(P)
local UserId = tostring(P.UserId);
-- Data[UserId].Info['Last Online'] = os.time();
local DataTable = Data[UserId];
if (not (DataTable)) then
return;
end;
saveData(P,DataTable);
PlayerData[UserId] = nil;
Data[UserId] = nil;
end);