Transferring Datastore script into a new improve one

I’m making a new datastore system to replace the old one but I don’t want player data to be lost.

Old script

local MyDataStore = DataStoreService:GetDataStore('t1')

game.Players.PlayerAdded:Connect(function(player)	
	
	
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = 'leaderstats'

	local currency = Instance.new("IntValue", leaderstats)
	currency.Name = 'currency1' 
	currency.Value = 0 

	local PlayerCashId = 'currency1'..player.UserId
	local CashData

	local s, err = pcall(function()
		CashData = MyDataStore:GetAsync(PlayerCashId)
	end)

	if s then
		currency.Value  = CashData
	else
		warn(err)
	end
	
	currency:GetPropertyChangedSignal("Value"):Connect(function()
		currency.Value = math.clamp(currency.Value, 0, math.huge)
	end)

end)
		
game.Players.PlayerRemoving:Connect(function(player)

	local PlayerCashId = 'currency1'..player.UserId

	local s, err = pcall(function()
		MyDataStore:SetAsync(PlayerCashId, player.leaderstats.currency1.Value)
	end)

	if s then
		print('Data Saved')
	else
		warn(err)
	end
end)

local suc,err = pcall(function()
	MyDataStore:RemoveAsync("UID HERE")
end)

New script


local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("t1")

game.Players.PlayerAdded:Connect(function(player)

	local currency = Instance.new("IntValue")
	currency.Name = "Currency"
	currency.Value = 0
	currency.Parent = player
	
	local data

	local success, errorMsg = pcall(function()
		data = DataStore:GetAsync(player.UserId)
	end)

	if data ~= nil then		
		if data.Currency then
			currency.Value = data.Currency
		end
	end

end)


game.Players.PlayerRemoving:Connect(function(player) 
	local data = {}
	data.Currency = player.Currency.Value

	local success,errorMsg = pcall(function()
		DataStore:SetAsync(player.UserId,data)
	end)	

	if success then
		print("Successfully Saved")
	end

end)


game:BindToClose(function() 
	for i, player in pairs(game.Players:GetPlayers()) do
		local data = {}
		data.Currency = player.Currency.Value

		local success,errorMsg = pcall(function()
			DataStore:SetAsync(player.UserId,data)
		end)
		if success then
			print("Successfully Saved")
		end
	end
end)
1 Like

you should be completely fine
all data will still be the same since in both scripts you are using the same datastore which is “t1”

if you were to change “t1” to a different name, then the data would reset since it is a completely new datastore

2 Likes

well it don’t work because the data didn’t transfer over

when a player joins, get their data from the old datastore and save it to the new one

What happened? All you’ve said is that it doesnt work, but not what happens

it just dont work because its didn’t get any data

but how im just confused not sure how this works at all

people seem to do this a lot, lol. Change it to

local data = nil

Btw, sorry for the late reply.

1 Like