Datastore Tutorial (Reduce data loss!)

Datastore 2 has progress loss issues for some reason, even the latest version

1 Like

You may not be using it right.

Is this a correct way of using? I’m very new to ds2 and it causes progress loss (mostly on shutdowns) for some reason.

game.Players.PlayerAdded:Connect(function(plr)
	local DataStore = DataStore2("Data", plr)
	local UserDataTable = DataStore:Get(Template)
	
	SharedDataStorage[plr] = UserDataTable

	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"	
	local Coin = Instance.new("NumberValue", leaderstats)
	
	local Methods = Instance.new("Folder", plr)
	Methods.Name = "Methods"

	
	Coin.Value = UserDataTable.Coins
	Coin:GetPropertyChangedSignal("Value"):Connect(function()
		if SharedDataStorage[plr] then
			SharedDataStorage[plr]["leaderstats"]["Coins"] = Coin.Value 
		end
	end)
	
	for i,z in pairs(SharedDataStorage[plr].Methods) do
		local item = Instance.new("StringValue", Methods)
		item.Name = z
	end

	Methods.ChildAdded:Connect(function(c)
		table.insert(SharedDataStorage[plr].Methods, c.Name)
		UpdateAllFolders:Fire(plr)
	end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local DataStore = DataStore2("Data", plr)
	DataStore:Set(SharedDataStorage[plr])
	SharedDataStorage[plr] = nil
end)

game:BindToClose(function()
	if game:GetService("RunService"):IsStudio() then return end

	for _,plr in pairs(game.Players:GetPlayers()) do
		local DataStore = DataStore2("DataKey", plr)
		DataStore:Set(SharedDataStorage[plr])
		SharedDataStorage[plr] = nil
	end
end)