Dataloss issue/datastore2 advice needed!

Hello,

Recently made a game and had a bit of a datastore issue. To try resolve this issue I’ve attempted to transfer over to Datastore2, which worked well for about a day and then there were more reports of dataloss.

Additionally, there have been reports of them returning to the game with additional data (??), extra XP and levels. I assumed it was the loops continuing to run after they’ve left the game but I’ve just ran a few tests with a lot of prints and they do appear to stop.

I just need a second opinion to help me figure out whats going wrong. I’ve spoken to a few friends who are advanced programmers and they are also a bit stuck on what to suggest. I’m sure I’m missing something, so if you have any suggestions I’ll be extremely grateful to hear them.

LOADING DATA


local dataSheet = {}
local simpleDataSheet = {
	["Galleons"] = 0,
	["House"] = "Unsorted",
	["Level"] = 1,
	["XP"] = 0,
	["WandMode"] = "RP",
	["RPName"] = "NOT_SET",
	["RPDesc"] = " ",
	["EquippedWand"] = "Basic",
	["EquippedBroom"] = "NONE",
	["RPTitle"] = "",
	["Detention"] = 0,
	["XPBoosted"] = 0,
	["GalleonBooster"] = 0,
	["DailyReward"] = 0,
	["DailyRewardBooster"] = 1,
	["OwnedWands"] = {"Basic"},
	["OwnedBrooms"] = {},
	["OwnedSpells"] = {},
	["Titles"] = {},
}

local function LoadData(player, DefaultData)
	local DataStore = DataStore2(DataStoreName, player)
	local Data = DataStore:Get(DefaultData)

	return Data
end

function LoadSheet(keyName,blankSheet,masterSheet)
	--\/--SIMPLE DATA
	local loadedData
	local success, err = pcall(function()
		loadedData = mainStorage:GetAsync(keyName..player.userId) or blankSheet 
-- the original datastore was losing data, so I just tried to salvage as much data per user as I could by obtaining their last save on the previous system, and using that as a 'default' table to pass.
	end)
	if err then
		loadedData = blankSheet
	else
		dataLoadedSuccessfully = true
	end
	
	local toreturn = LoadData(player,loadedData)
	--\/--FILLIN IN NEW VALUE
	for key,value in pairs(blankSheet) do
		if not toreturn[key] then
			toreturn[key] = value
		end
	end
	return toreturn
end

SAVING DATA

function saveAllData(leaving)
	local DataStore = DataStore2(DataStoreName, player)
	DataStore:Set(dataSheet)
	DataStore:Save()
	print("saving: "..tostring(leaving))
	if leaving == true then
		timeToEnd = true -- end loops for regular xp rewards
	end
	return "SAVE COMPLETE"
end

ADDITIONAL INFO
-XP is rewarded every 10 mins, I call saveAllData() then as a precautionary save.
-Every dev-product purchase for money triggers a save too, just to be safe.
-When a player leaves, I call saveAllData()
-BindToClose is has also been set up.
-Not had a report of data being entirely lost. Just a few levels, spells, bought items or money missing. No one has experienced a back-to-default loss.

Any advice on how to locate any errors I may be missing or how to improve this code would be extremely appreciated. If you need anymore context surrounding code I’ll provide!

Thanks!

1 Like

What’s dataSheet here? It’s not defined in the function, so it seems like it’s not player specific.

2 Likes
local dataSheet = {}

Is right above simpleDataSheet

All of this code is from a module which is cloned and filled in with each players specific information when they join. When a player leaves, their data is sent to save and then the module is deleted once saving has completed. (not the most efficient method I know, but this was my first attempt at any store like this and I thought it would be the easiest way of obtaining and writing over data for me, personally.)

The return in the LoadSheet function sets “dataSheet” variable to the loaded data

1 Like

Hey sorry that it is not 100% related to your problem but take it as a small advice, I came across DataStore2 too but I was not too satisfied as with ProfileService that is so easy to set up, understand and also to work with - so things such as saving tables are really easy!

Link to it here:

1 Like

Thanks so much Ill look into this now!!

1 Like