How can my DataStore1 -> DataStore2 system be improved?

Hello! How is it going? I created a system (Which works, tested on multiple accounts), and one of my beta testers said that his data didn’t transfer properly.

Is there any way to improve my system? Please let me know. Thanks! WE

local DataStore2 = require(game:GetService("ServerScriptService"):WaitForChild("DataStore2"))
local DataStoreService = game:GetService("DataStoreService")

local CharactersDataStore = DataStoreService:GetDataStore("Characters")
local TheDatastore = DataStore2.Combine("Characters", "CharactersBackup")

local TeleportService = game:GetService("TeleportService")

game:GetService("Players").PlayerAdded:Connect(function(Player)
	local DataStore2Characters = DataStore2("Characters", Player)
	local CharacterData2and1
	local CharacterData3
	
	
	local Success, Error = pcall(function()
		CharacterData3 = DataStore2Characters:GetTable({
			Characters = {""},
			EquippedCharacter = "",
			leaderstats = {{Points = 50}}
		})
	end)
	
	local Success, Error = pcall(function()
		CharacterData2and1 = CharactersDataStore:GetAsync(Player.UserId)
	end)
	
	if Error then
		error(Error)
	end
	
	CharactersDataStore:RemoveAsync(Player.UserId)
	
	local success, output = pcall(function()
		DataStore2Characters:Set(CharacterData2and1)
	end)

	if not success then
		error(output)
	end
	
	print(DataStore2Characters:GetTable({
		Characters = {""},
		EquippedCharacter = "",
		leaderstats = {{Points = 50}}
	}).EquippedCharacter)
	print(DataStore2Characters:GetTable({
		Characters = {""},
		EquippedCharacter = "",
		leaderstats = {{Points = 50}}
	}).Characters[1])
	
	
	TeleportService:Teleport(6871053299, Player)
	
end)
1 Like

Ive found a few things which I hope solve this issue

You are removing the data outside of the pcalls.
Data should only be removed uppon success

The pcalls do not try again should it error meaning that if it fails to get data it will save a nil value. (Along with the last issue this would make data loss certain when it errors)

How to properly do it

for I = 1, 5
local Success, Error = pcall(function()
– Get Data
if Data then
– Save new data
– Remove old data
end
end

if Success then
    break - Stops the loop
else
    print(Error)
end

wait(3)

end

DataStore2 handles pcalls for you. It retries and everything, if it all fails, it should make a mock for your data, so it will be like “There wasn’t any data!” But it won’t overwrite the previous data when the player leaves.

You can detect whether it’s failed and mock data or not by using :IsBackup() on a store.

1 Like