DataStore2 ServerShutdown/Crash Saving Issue

I have a DataStore2 saving system for my game, and it work’s perfectly, until a player either crashes on their own accord or I shutdown the server that player is in manually. If a player leaves my game the data saves, but if a crash/servershutdown happens they lose all the data they gained/changed from when they joined.

This is the bulk of my code(Only showing one value since the rest is rinse and repeat):

game.Players.PlayerAdded:Connect(function(player)
	local Holder = Instance.new("Folder")
	Holder.Name = "Data"
	Holder.Parent = player

	local Value1 = Instance.new("NumberValue")
	Value1.Name = "Strength"
	Value1.Parent = Holder	

	DataStore2.Combine("Player Test", "Strength", "Toughness", "Stamina", "Breath", "Race", "Yen", "StatusLevel", "Hair2",
		"PassFS", "Hair", "HairColor", "Eyes", "EyeColor", "Mouth", "AffinityColor", "Clothes", "HiltColor1", "WeaponSheathColor",
		"BreathMastery", "Ability", "Skin", "Brows", "Acc", "EquipedSlotWeapon", "EquipedSlotHaori", "EquipedSlotAcc1", "EquipedSlotAcc2", "Rank",
		"InvSlot1", "InvSlot1A", "InvSlot2", "InvSlot2A", "InvSlot3", "InvSlot3A", "InvSlot4", "InvSlot4A", "InvSlot5", "InvSlot5A", "InvSlot6",
		"InvSlot6A", "InvSlot7", "InvSlot7A", "InvSlot8", "InvSlot8A", "InvSlot9", "InvSlot9A","InvSlot10", "InvSlot10A", "InvSlot11", "InvSlot11A",
		"InvSlot12", "InvSlot12A", "InvSlot13", "InvSlot13A", "InvSlot14", "InvSlot14A", "InvSlot15", "InvSlot15A", "InvSlot16", "InvSlot16A", "Age",
		"AgeTime","Family", "CharName", "CharStart", "HairDetail", "HiltColor2", "HiltColor3", "RankExp", "RankReq")

-------------------------------------------------
local StrengthStore = DataStore2("Strength", player)
			
			if StrengthStore:Get() == nil then
				StrengthStore:Set(1)
			end
			
		Value1.Value = StrengthStore:Get()
		
		
local function StrengthC(value)	
	if Value1 ~= nil and Value1.Value ~= nil then	
	StrengthStore:Set(Value1.Value)
	end
end

Value1.Changed:Connect(StrengthC)
------------------------------------------------- 
end)
2 Likes

There’s an event of the Players service called PlayerDisconnecting. Perhaps you could make it save on that event.
More info here:

PlayerDisconnecting is an internal function not established for developer use. You are meant to use PlayerRemoving. In addition to this, DataStore2 automatically handles leaving conditions for saving, so this is out of the question to begin with.

The most appropriate applicable fixes here are to mass save with BindToClose (provided that even works - it probably will if OP doesn’t use the Player instance as a data dependency) and/or implement an autosaving feature every so often, or push data to a cache upon changing (this is provided DataStore2 doesn’t actually use a save operation when running Set).

Thanks, I ended up using the BindToClose with Save:() and it worked, was not aware DataStore2 had a Save:() function, and that Set:() only saved on leave.(Which is my bad for not thoroughly looking at all the functions that come with DataStore2)