How to transfer Datastore from a old script to a new script

What I’m trying to achieve: I’m trying to transfer a datastore to a new and improved data store script with the player data since the old datastore script was poorly made.

I made a post earlier about this but the responders weren’t helpful at all and didn’t reach back to me.
It would be greatly appreciated if someone helps me out.

old script

local MyDataStore = DataStoreService:GetDataStore('DATASTORES')

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = 'leaderstats'
	local currency = Instance.new("IntValue", leaderstats)
	currency.Name = 'cash' 
	currency.Value = 0 
	local PlayerCashId = 'cash'..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)

New

local DataStore = DataStoreService:GetDataStore('DATASTORES')

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

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

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

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

end)

What do you mean transfer? If you’re getting the same Datastore with :GetDataStore() it should share the same info between scripts automatically.

transfer player data which is cash

If their data is already stored, you can use the same DataStore with the new script and everything should “transfer”.

1 Like

that’s not the case then because it didn’t transfer and the value remains 0.

Then, there could be something wrong with the data save script, have you tried the data store script out?

yea and it seems to work properly.

Hey, you forgot to parent the int value to leaderstats in your new data store script.

local leaderstats = Instance.new(“Folder”, player)
leaderstats.Name = ‘leaderstats’

1 Like

Didn’t really forgotten about that I just removed it since it was a bit unnecessary. So if that’s the problem how do I fix this?

Here’s the edited version from me:

local DataStore = DataStoreService:GetDataStore('DATASTORES')

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”, player)
leaderstats.Name = ‘leaderstats’

	local currency = Instance.new("IntValue")
	currency.Name = "cash"
	currency.Value = 0
	currency.Parent = leaderstats
	
	local data = nil

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

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

end)

the leaderstats is very necessary, when you want to make a leaderstats because if you don’t have it, the value won’t pop up in the leaderboard.

1 Like

In the game I’m working on I have the leaderboard disabled so in that circumstances, I really don’t need it. The edit does work but It would be great if there’s some other way.

Wait, I just realized something. You have to use the same key. Use the key in the old script for the new script.

1 Like

I do but for which script? the old or new?

Use the key in the old script for the new script.

This line would be:

DataStore:GetAsync("cash"..player.UserId)

I have done that what should I do though.

that’s all, try to see if it works now

result in a index number error

You gotta do the same thing for the player removing event

1 Like

Screenshot 2021-07-03 201201
there’s still a error involving this line right here