How can I switch from DataStore to DataStore2 without losing data?

Well I have a datastore at the moment but I need to move it to Datastore2 without losing data, can anyone help me?

if you can I will pass what you need by DM

You cannot just take and transfer data, but you can check it when receiving data, if there is no data from DataStore2, then take it from DataStore and save it in DataStore2.

2 Likes

I understand but if I do that I would not lose data right?

Can u help me please? I swear the stress is hurting me.

Can you send your current code?

1 Like

Sure, look

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("MoneyStats")



local function onPlayerJoin(player)  -- Runs when players join

	local leaderstats = Instance.new("Folder")  --Sets up leaderstats folder

	leaderstats.Name = "leaderstats"

	leaderstats.Parent = player



	local money = Instance.new("IntValue") --Sets up value for leaderstats

	money.Name = "Money"

	money.Parent = leaderstats



	local exp = Instance.new("IntValue") --Sets up value for leaderstats

	exp.Name = "Delivers"

	exp.Parent = leaderstats



	local playerUserId = "Player_" .. player.UserId  --Gets player ID

	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data

	if data then

		money.Value = data['Money']

		exp.Value = data['Delivers']

	else

		-- Data store is working, but no current data for this player

		money.Value = 12000

		exp.Value = 0

	end

end



local function create_table(player)

	local player_stats = {}

	for _, stat in pairs(player.leaderstats:GetChildren()) do

		player_stats[stat.Name] = stat.Value

	end

	return player_stats

end



local function onPlayerExit(player)  --Runs when players exit



	local player_stats = create_table(player)

	local success, err = pcall(function()

		local playerUserId = "Player_" .. player.UserId

		playerData:SetAsync(playerUserId, player_stats) --Saves player data

	end)



	if not success then

		warn('Could not save data!')

	end

end



game.Players.PlayerAdded:Connect(onPlayerJoin)

game.Players.PlayerRemoving:Connect(onPlayerExit)

Please tell me you can help me

Sorry it took me so long to be busy, try this if you want to use tables as a database (Keep in mind that the Increment function won’t work with them)

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("MoneyStats")
local DataStore2 = require(path.to.DataStore2)
DataStore2.Combine("DATA", "MoneyStats")

local function create_table(player)
	local player_stats = {}
	for _, stat in pairs(player.leaderstats:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end
	return player_stats
end

local function onPlayerJoin(player)
	local MoneyStats = DataStore2("MoneyStats", player)
	local data = MoneyStats:Get()
	if not data then --if datastore2 im empty for player
		data = playerData:GetAsync("Player_"..player.UserId) --get from datastore
		if not data then data={Money=12000, Delivers=0} end --if this a new player and have in datastore set default values
		MoneyStats:Set(data) --save to datastore2
	end
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = leaderstats

	local exp = Instance.new("IntValue")
	exp.Name = "Delivers"
	exp.Parent = leaderstats

	money.Value = data['Money']
	exp.Value = data['Delivers']
	
	money.Changed:Connect(function(value) --update DataStore2 when changed value
		MoneyStats:Set(create_table(player))
	end)
	
	exp.Changed:Connect(function() --update DataStore2 when changed value
		MoneyStats:Set(create_table(player))
	end)
end


local function onPlayerExit(player)
	local player_stats = create_table(player)
	local MoneyStats = DataStore2("MoneyStats", player)
	local success, err = pcall(function()
		MoneyStats:Set(player_stats)
		MoneyStats:Save() --save to datastore2
	end)

	if not success then
		warn('Could not save data!')
	end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
1 Like

My God thank you very much but one question, if I use this script I will not lose the data of my players that I have at the moment?

It would really get me into serious trouble since there are more than 7 million players.

You are my salvation. THANK YOU FOR EXISTING.

No, you can always refer to the old database

1 Like

God thank you so much, do you know how I can increase the money?

In what sense? You just change the Money value of the player in the folder

1 Like

Ohh so if I put the script everything will work correctly right?

Should, if something does not work - let me know

1 Like

Thank you a LOT men seriously!

Uh I put a print at the end to know if it is running with Datastore2 but I don’t see the print in console am I doing something wrong? :c

Can you be more precise? What does not work

1 Like

I think isnt working ill record an video 4 you give me a sec please