Need urgent help with data saving

So I have a MASSIVE problem,
My game with 150+ concurrent players is struggling to save data, and REVERSING players data!

These are some screenshots of one of the games players IN ORDER of them posting them.
image
image
image

As you can see, this player LOST data overtime, and he claimed:
“I rejoined multiple times, and only sometimes did I lose data”

Now, you CANNOT lose ANY of the first number, there’s simply no code in the game to do that.

I have also reviewed all the code in the game, and there is no code to remove from or reset any data.

This is the code I’m using for saving:

function SavePlayerData(Player, Id, BindToClose)
	local DataToSave = {}
	local Inventory = {}

	for i, v in pairs(Player.leaderstats:GetChildren()) do
		DataToSave[v.Name] = v.Value
	end
	for i, v in pairs(Player.Upgrades:GetChildren()) do
		DataToSave[v.Name] = v.Value
	end
	for i, v in pairs(Player.TrailInventory:GetChildren()) do
		Inventory[v.Name] = v.Name
	end
	
	if not Id and not table.find(Blacklist, Player.UserId) then -- Player = Player Instance
		print("Saving data... (Plr Inst")
		DS:UpdateAsync(Player.UserId.."DATA", function()
			return DataToSave
		end)
		DS2:UpdateAsync(Player.UserId.."DATA", function()
			return Inventory
		end)
	elseif Id and not table.find(Blacklist, Player.Name) then -- Player = Folder Instance
		print("Saving data... (Cahce)")
		DS:UpdateAsync(Player.Name.."DATA", function()
			return DataToSave
		end)
		DS2:UpdateAsync(Player.Name.."DATA", function()
			return Inventory
		end)
	end
	print("Data saved!")
	if not Id then table.remove(SavingDataIds, table.find(SavingDataIds, Player.UserId)) end
end

-- deleted some code that wasn't relevant

And these are the 2 only bits of code in the whole script to call the “SavePlayerData” function.

game.Players.PlayerRemoving:Connect(function(Player)

	SavePlayerData(Player, false, false)
	
end)

game:BindToClose(function()
	print('Saving data for all players..')
	for Index, Player in pairs(ServerCache:GetChildren()) do
		if not SavingDataIds[Player.Name] then
			SavePlayerData(Player, true, true)
		end
		task.wait()
	end
	print('Saved all data!')
end)

-- deleted some code that wasn't relevant

Going to add on, the problem seems to only be with some of the players, not everyone. Me for example, can jump between servers and my data will be perfectly fine.

3 Likes

My first thought is the user is rapidly leaving and rejoining, meaning the datastore has not updated before being loaded on a new server, so is fetching old info.
A laggy connection could be the root cause, or multiple clients.
Potentially calling the loadasync with a delay could overcome this but would have longer load times, or using a separate boolvalue in the player’s data table to prevent the data loading until the save data has updated, this would also prevent the same user data being uploaded on multiple clients.

Are you initialising new player data with a blank/default table? As if the loadasync fails it would then load this into the game, and then presumably save over any previous data when players left.

1 Like

We tested this, and its not the cause. Plus, my problem is different to what that would cause.

My problem exactly is that the player is slowly losing data overtime, not getting a complete reset.

Adding on, the issues reported, the way I found out about this were done over 2 days with no spam rejoining.

1 Like

This video might help Advanced Roblox Scripting Tutorial #13 - Data Store / Saving Player Data (Beginner to Pro 2019) - YouTube

1 Like

I already have a good understanding of data stores, in fact I would say the system I use on this game is pretty flawless.

My issue is again,

1 Like

yea nah nvm about this post, im just dumb :skull:

I forgot to delete the cache when the player left.

1 Like

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