How should I transfer one DataStore to another?

  1. What do you want to achieve? I’d like to transfer some data from a DataStore to a different key.

  2. What is the issue? The system I used saves datastores with the Player’s name, but that’s not very good, because if someone changes their username, they will lose all their stats.

  3. What solutions have you tried so far? I haven’t seen any posts about this on the DevForum, so I posted this.

Here is my script:

game.Players.PlayerRemoving:connect(function(player)
	local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")
	local statstorage = player:FindFirstChild("leaderstats"):GetChildren()
	for i =  1, #statstorage do
		datastore:SetAsync(statstorage[i].Name, statstorage[i].Value)
	end
end)

game.Players.PlayerAdded:connect(function(player)
	local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")
	player:WaitForChild("leaderstats")
	wait(1)
	local stats = player:FindFirstChild("leaderstats"):GetChildren()
	for i = 1, #stats do
		if stats[i].Name ~= "Points" then
			stats[i].Value = datastore:GetAsync(stats[i].Name)
		end
	end
end)
1 Like

You should probably use Player.UserId then. That way, you have unique values for every player that won’t change for any account.

I’m not an experienced user of DataStores, so I’ll just give help with what I can, using tables as an example.

local oldTable = {"K", "E", "L", "P"}
local newTable = {}

Let’s say you want to move the contents of oldTable into newTable. The most straightforward way I’d do it is like so:

for i, v in pairs(oldTable) do
    newTable[i] = oldTable[i]
end

Linearly search through oldTable, and make every element in newTable the same as in oldTable. If you had custom indexes/keys in oldTable, then instead of using in pairs, you’d want to use in ipairs.

I realize this isn’t what you need exactly, but I hope it helps somewhat; however, the principle of it can still apply.

So, I take it that you already have DataStores with player names saved? I’ve been reading this, and this, and this, which I’d thought could be of help. The problem is, however, there doesn’t seem to be a way to retrieve all the data from a DataStore without use of something like OrderedDataStore.

1 Like