Change my datastores without losing peoples data

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.

1 Like

I need to fix this so please, if you can help reply.

You have two paths ahead of you depending on your game’s unique visits/datastore keys:

  1. If a few unique users(lets say less than a million) played the game, you can shutdown the game for a bit and perform a “data migration process”. Basically write and run code that loops through all the keys of the datastore rebirths, and for each of those keys it fetches cash and combine them into a single datastore(using the same key). If your game has many users(more than a million) you can also consider doing this process for your most frequent players(assuming you know who they are, for example they may be on leaderboards).
  1. If your game has a large player-base, you will have to solve the problem dynamically. Basically for each user that joins, fetch the new datastore and if data is there, load that. If not, then load the old datastore, if data isn’t there either assume the user is a new user and set data in the new datastore. Else mitigate their data from old to new datastore. This process will only occur once per unique user(because after that the first request will always find data) so duplicate datastore requests should not stress your game that much.
1 Like

You can make script that loads data from these 3 (or 2 if you will merge it into one existing) datastore’s and then from these 2 (or this 1) takes data and puts into another

Basically a script that just takes data from one place and puts it to another place

(Another person told basically same)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.