Datastore2 Reverting issue

I made a simple game but when i tried moving a value from Datastore2 into an OrderedDatastore, everytime i save the data with Datastore2 with the removed value it now doesn’t work.

Heres the script:

local Datastore2 = require(game.ReplicatedStorage.DataStore2)
local DatastoreService = game:GetService("DataStoreService")

local DS2Key = "..."
local Datastore: OrderedDataStore = DatastoreService:GetOrderedDataStore(Key)

local MainKey = "GAME"
local Key = "GAME_Main"

Datastore2.Combine(MainKey, Key)

local DataVersions = {}

local function GetA(Player)
	local Key = ("Player_%i"):format(Player.UserId)
	local A
	local Success,Error = pcall(function()
		A = Datastore:GetAsync(Key)
	end)
	if not Success then
		warn(Error)
		Player:Kick("Error when getting data :/")
		return
	end
	
	if not A then A = 0 end
	
	warn(Player, "a is",a, "| Key is",Key)
	
	return a
end
game.Players.PlayerAdded:Connect(function(player)
	local ls = Instance.new("Folder")
	ls.Name = "leaderstats"

	local A = Instance.new("IntValue")
	A.Name = "A"
	A.Parent = ls
	
	ls.Parent = player

	local Datastore = Datastore2(DS2Key, player)
	local A = GetA(player)

	local data = Datastore:Get({
		["data1"] = 0;
		["data2"] = 0;
		["data3"] = {};
		["data version"] = 0;
	})
	
	DataVersions[player] = data["data version"] or 0
	
	print("Data loaded:",a, data)

	if data.A then -- keep old data
		A = data.A
	end

	a.Value = a

game.Players.PlayerRemoving:Connect(function(player)
	local Key = ("Player_%i"):format(player.UserId)
	
	local A = player.leaderstats.a.Value
	local OldA = GetA(player)

	-- Save A

	if A ~= OldA then
		local i = 0
		local Success, Error
		repeat
			Success, Error = pcall(function()
				Datastore:SetAsync(Key, a)
			end)
			if not Success then
				warn(Error,"at iteration",i)
			else
				print("Success saving A data!",player,A)
			end
			i += 1
		until Success or i == 4
	else
		print("Didnt save A because it didnt change")
	end

	local data = {
		["data1"] = player.leaderstats.data1.Value;
		["data2"] = player.leaderstats.data2.Value;
		["data3"] = data3[player];
		["data version"] = DataVersions[player] + 1;
	}
	
	local datastore = Datastore2(DS2Key, player)
	if datastore:Get({}) == data then print("Data is the same.") return end
	datastore:Set(data)
	print("Saved player main data as",data)
end)

I removed some unnecessary bits and subsituted the data that was moved into an OrderedDatastore for the letter “A”

My main problem is that Datastore2 rolls back, which is unwanted behaviour for me. It wont let me remove the A field or it will not save the data for the session.

1 Like

Is there an error in the console?

No, but I have since found the solution, it was because DataStore2 was automatically saving before my Set script could load, so by the time it had set the data to be saved, it had already been saved. I first tried adding a Datastore:Save() function afterwards and it worked perfectly. I then moved the datastore2 saving above the A saving and it worked without the Save call

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.