Hey, I have a script with 2 datastores in it and I need to turn them into 1 datastore without losing all the data.
My problem is that if you leave and both values, Rebirth and Cash, have changed, it won’t save anything.
Is it possible for me to transfer it to just 1 datastore without losing all the data on them so players won’t lose their progress?
Here is the code:
local service = game:GetService("DataStoreService")
local datastore = service:GetOrderedDataStore("CashStore")
local rebirthstore = service:GetOrderedDataStore("RebirthStore")
local player = nil
game.Players.PlayerAdded:Connect(function(plr)
player = plr
local folder = Instance.new("Folder", plr)
folder.Name = "leaderstats"
local cash = Instance.new("NumberValue", folder)
cash.Name = "Cash"
local rebirths = Instance.new("NumberValue", folder)
rebirths.Name = "Rebirths"
local ball = Instance.new("StringValue", folder)
ball.Name = "Ball"
ball.Value = "Default"
local successCash, playercash = pcall(function()
return datastore:GetAsync(plr.UserId)
end)
local successRebirth, playerrebirth = pcall(function()
return rebirthstore:GetAsync(plr.UserId)
end)
if successCash and playercash then
cash.Value = playercash
else
cash.Value = 100
end
if successRebirth and playerrebirth then
rebirths.Value = playerrebirth
else
rebirths.Value = 0
end
end)
local function savePlayerData(plr)
local success, err = pcall(function()
datastore:SetAsync(plr.UserId, plr.leaderstats.Cash.Value)
rebirthstore:SetAsync(plr.UserId, plr.leaderstats.Rebirths.Value)
end)
if not success then
warn("Failed to save data for player: " .. plr.Name .. ". Error: " .. err)
end
end
game.Players.PlayerRemoving:Connect(function(plr)
savePlayerData(plr)
end)
game:BindToClose(function()
savePlayerData(player)
end)
Any help is appreciated, Thank you.