--[[**
	<description>
	Saves the data to the data store. Called when a player leaves.
	</description>
**--]]

function DataStore:Save()
	if not self.valueUpdated then
		
		return
	end

	if RunService:IsStudio() and not SaveInStudio then
	
		if not SaveInStudioObject then
			
		end
		return
	end

	if self.backup then

		return
	end

	if self.value ~= nil then
		local save = clone(self.value)

		if self.beforeSave then
			local success, newSave = pcall(self.beforeSave, save, self)

			if success then
				save = newSave
			else
			
				return
			end
		end

		if not Verifier.warnIfInvalid(save) then return  end

		local success, problem = self.savingMethod:Set(save)

		if not success then
			-- TODO: Something more robust than this
			error("save error! " .. tostring(problem))
		end

		for _, afterSave in pairs(self.afterSave) do
			local success, err = pcall(afterSave, save, self)

			if not success then
			
			end
		end

	
	end
end
