How to transfer on normal data store to data store 2?

Im having an big bug which is Data Loss, how can i transfer this script to data store 2.

local DataStoreService = game:GetService("DataStoreService") 
local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player) 
	local leaderstats = Instance.new("Folder",player) 
	leaderstats.Name = "leaderstats" 
	
	local Coins = Instance.new("IntValue", leaderstats) 
	Coins.Name = "Coins"
	Coins.Value = 0 
	
	local playerUserId = "Player_"..player.UserId -- User
	
	--Load Data
	local data
	local sucess, errormessage = pcall(function()
		wait(0.05)
		data = myDataStore:GetAsync(playerUserId) 
	end)
	
	Coins.Value = data
	
	if sucess then -- if sucess then
		
		data = myDataStore:GetAsync(playerUserId)
		
	elseif not sucess then
		
		player:Kick("Your coins data dont load properly try to rejoin.")
	end
	
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	wait(0.05)
	local playerUserId = "Player_"..player.UserId 
	
	local data = player.leaderstats.Coins.Value
	
	local sucess, errormessage = pcall(function()
		wait(0.05)
		myDataStore:SetAsync(playerUserId, data)
	end)
	
	if sucess then
		wait(0.05)

	else
		wait()
		warn(playerUserId.. " There was an error while saving the data of coins.")
		wait(0.05)
		myDataStore:SetAync(playerUserId, data)
	end
end)

I want to transfer on Ds2 without deleting data on normal data stores!

1 Like

Transfer your SetAsync to UpdateAsync

More about it here:

They want to convert the DataStore to DataStore2, not change the method of saving.

Heres new script :

local function save(player)
		
	
	local playerUserId = "Player_"..player.UserId 
	
	local data = player.leaderstats.Coins.Value
	
	local sucess, errormessage = pcall(function()
		wait(0.05)
		myDataStore:UpdateAsync(playerUserId, function(OldData)
			return data
		end)
	end)
	
	if sucess then
		wait(0.05)

	else
		warn(playerUserId.. " There was an error while saving the data of coins.")
		wait(0.05)
	
		
		end
	end

game:BindToClose(function()
	for i , v in pairs(game.Players:GetPlayers()) do
		save(v)
	end
end)

can it prevent data los?

Yeah atleast it does for me maybe try to put the data to {player.leaderstats.Coins.Value} but idk if it affects anything.

Why are you yielding with wait?