Hello! I have modified my data-storage format in my Game, however, I noticed that it deletes the Data of players with the old format and does not convert it.
Here is how the format looked before:
{Coin=_,Token=_,XP=_,Level=_,Playtime=_,Streak=_,GameJoinD=_}
Here is how the new format looks like:
{Currency={same parameters as above},Inventory{inventory items go here}}
There might be a silly mistake in the conversion that I haven’t yet realised. But here is the code:
local RepStore = game:GetService("ReplicatedStorage")
local SvrStore = game:GetService("ServerStorage")
local DatStore = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local PlayerDataFolder = RepStore.PlayerData
local InfoStore = DatStore:GetDataStore("PlayerData")
local ToCall = game.ServerStorage.Events.DataCall
local ServerStore = DatStore:GetDataStore("ServerStore")
local CurFormat = {"Level","Token","Xp","Coin","Playtime","Streak","GameJoinD"}
local Conversions = {Score="Xp"}
local function UnloadPlayerData(Player)
local IDList = ServerStore:GetAsync("IDlist")
if not IDList then IDList = {} end
if not table.find(IDList,Player.UserId) then
table.insert(IDList,Player.UserId)
ServerStore:SetAsync("IDlist",IDList)
end
local PlayerID = Player.UserId
local suc, err = pcall(function()
local PlayerData = InfoStore:GetAsync(PlayerID)
if not PlayerData then -- adds player data
warn("data empty")
PlayerData = {}
PlayerData.Inventory = {}
PlayerData.Currency = {}
local Currency = PlayerData.Currency
local Inventory = PlayerData.Inventory
for i, v in pairs(CurFormat) do -- goes through the format
if v == "GameJoinD" then
Currency[v] = os.time() -- os date
else
Currency[v] = 0
end
end
end
local PlayerFolder = Instance.new("Folder")
local CurrencyFolder = Instance.new("Folder")
local InventoryFolder = Instance.new("Folder")
PlayerFolder.Name = PlayerID -- adds currency and inventory folder
PlayerFolder.Parent = PlayerDataFolder
CurrencyFolder.Name = "Currency"
CurrencyFolder.Parent = PlayerFolder
InventoryFolder.Name = "Inventory"
InventoryFolder.Parent = PlayerFolder
if not PlayerData.Currency and PlayerData.Inventory then -- adds ^
PlayerData.Currency = {}
PlayerData.Inventory = {}
end
local Currency = PlayerData.Currency
local Inventory = PlayerData.Inventory
for i, v in pairs(PlayerData) do -- converts old system to new
if table.find(CurFormat,i) then
local suc, err = pcall(function()
Currency[i] = v
PlayerData[i] = nil
end)
end
end
for i, v in pairs(CurFormat) do
if not Currency[v] then
if v == "GameJoinD" then
local timeS = os.time()
Currency[v] = timeS
else
Currency[v] = 0
end
end
end
for i, v in pairs(Currency) do
if Conversions[i] then
local Conv = Conversions[i]
Currency[Conv] = v
Currency[i] = nil
end
if not table.find(CurFormat,i) then
PlayerData[i] = nil
end
end
for store, data in pairs(Currency) do
local IntHolder = Instance.new("IntValue")
IntHolder.Name = store
IntHolder.Value = data
IntHolder.Parent = CurrencyFolder
end
for object, amount in pairs(Inventory) do
local IntHolder = Instance.new("IntValue")
IntHolder.Name = object
IntHolder.Value = amount
IntHolder.Parent = InventoryFolder
end
end) if not suc then warn(err) end
end
local function PackPlayerData(Player)
local PlayerID = Player.UserId
if PlayerID then
local PlayerFolder = PlayerDataFolder:FindFirstChild(PlayerID)
local success, err = pcall(function()
if PlayerFolder then
local PlayerData = {}
local CurrencyFol = PlayerFolder:FindFirstChild("Currency")
local InventoryFol = PlayerFolder:FindFirstChild("Inventory")
PlayerData.Currency = {}
PlayerData.Inventory = {}
local CurrencyV = PlayerData.Currency
local InventoryV = PlayerData.Inventory
for i, v in pairs(CurrencyFol:GetChildren()) do
CurrencyV[v.Name] = v.Value
end
for i, v in pairs(InventoryFol:GetChildren()) do
InventoryV[v.Name] = v.Value
end
InfoStore:SetAsync(PlayerID,PlayerData)
end
end)
if success then
PlayerFolder:Destroy()
else
warn(err)
end
end
end
local function TranslateCall(callData)
coroutine.wrap(function()
local suc, err = pcall(function()
for i, v in pairs(callData) do
local PlrId = v.PlrId
local Ttype = v.Type
local value = v.Value
local LPlrFol = PlayerDataFolder[PlrId]
local SVal = LPlrFol.Currency[Ttype]
SVal.Value += value
end
end) if not suc then warn(err) end
end)()
end
Players.PlayerAdded:Connect(UnloadPlayerData)
Players.PlayerRemoving:Connect(PackPlayerData)
ToCall.Event:Connect()