RunService.Heartbeat:Connect(function()local N=#AutoSaveList if N>0 then local k=SETTINGS.AutoSaveProfiles/N local z=os.clock()while z-LastAutoSave>k do LastAutoSave=LastAutoSave+k local L=AutoSaveList[AutoSaveIndex]if z-L._load_timestamp<SETTINGS.AutoSaveProfiles then L=nil for k=1,N-1,1 do AutoSaveIndex=AutoSaveIndex+1 if AutoSaveIndex>N then AutoSaveIndex=1 end L=AutoSaveList[AutoSaveIndex]if z-L._load_timestamp>=SETTINGS.AutoSaveProfiles then break else L=nil end end end AutoSaveIndex=AutoSaveIndex+1 if AutoSaveIndex>N then AutoSaveIndex=1 end if L~=nil then(coroutine.wrap(SaveProfileAsync))(L)end end end if ProfileService.CriticalState==false then if#IssueQueue>=SETTINGS.IssueCountForCriticalState then ProfileService.CriticalState=true ProfileService.CriticalStateSignal:Fire(true)CriticalStateStart=os.clock()warn("\091\080\114\111\102\105\108\101\083\101\114\118\105\099\101\093\058 \069\110\116\101\114\101\100 \099\114\105\116\105\099\097\108 \115\116\097\116\101")end else if#IssueQueue>=SETTINGS.IssueCountForCriticalState then CriticalStateStart=os.clock()elseif os.clock()-CriticalStateStart>SETTINGS.CriticalStateLast then ProfileService.CriticalState=false ProfileService.CriticalStateSignal:Fire(false)warn("\091\080\114\111\102\105\108\101\083\101\114\118\105\099\101\093\058 \067\114\105\116\105\099\097\108 \115\116\097\116\101 \101\110\100\101\100")end end while true do local N=IssueQueue[1]if N==nil then break elseif os.clock()-N>SETTINGS.IssueLast then table.remove(IssueQueue,1)else break end end end)

RunService.Heartbeat:Connect(function()
	-- 1) Auto saving: --
	local auto_save_list_length = #AutoSaveList
	if auto_save_list_length > 0 then
		local auto_save_index_speed = SETTINGS.AutoSaveProfiles / auto_save_list_length
		local os_clock = os.clock()
		while os_clock - LastAutoSave > auto_save_index_speed do
			LastAutoSave = LastAutoSave + auto_save_index_speed
			local profile = AutoSaveList[AutoSaveIndex]
			if os_clock - profile._load_timestamp < SETTINGS.AutoSaveProfiles then
				-- This profile is freshly loaded - auto-saving immediately after loading will cause a warning in the log:
				profile = nil
				for i = 1, auto_save_list_length - 1 do
					-- Move auto save index to the right:
					AutoSaveIndex = AutoSaveIndex + 1
					if AutoSaveIndex > auto_save_list_length then
						AutoSaveIndex = 1
					end
					profile = AutoSaveList[AutoSaveIndex]
					if os_clock - profile._load_timestamp >= SETTINGS.AutoSaveProfiles then
						break
					else
						profile = nil
					end
				end
			end
			-- Move auto save index to the right:
			AutoSaveIndex = AutoSaveIndex + 1
			if AutoSaveIndex > auto_save_list_length then
				AutoSaveIndex = 1
			end
			-- Perform save call:
			-- print("[ProfileService]: Auto updating profile - profile_store_name = \"" .. profile._profile_store._profile_store_name .. "\"; profile_key = \"" .. profile._profile_key .. "\"")
			if profile ~= nil then
				coroutine.wrap(SaveProfileAsync)(profile) -- Auto save profile in new thread
			end
		end
	end
	-- 2) Issue queue: --
	-- Critical state handling:
	if ProfileService.CriticalState == false then
		if #IssueQueue >= SETTINGS.IssueCountForCriticalState then
			ProfileService.CriticalState = true
			ProfileService.CriticalStateSignal:Fire(true)
			CriticalStateStart = os.clock()
			warn("[ProfileService]: Entered critical state")
		end
	else
		if #IssueQueue >= SETTINGS.IssueCountForCriticalState then
			CriticalStateStart = os.clock()
		elseif os.clock() - CriticalStateStart > SETTINGS.CriticalStateLast then
			ProfileService.CriticalState = false
			ProfileService.CriticalStateSignal:Fire(false)
			warn("[ProfileService]: Critical state ended")
		end
	end
	-- Issue queue:
	while true do
		local issue_time = IssueQueue[1]
		if issue_time == nil then
			break
		elseif os.clock() - issue_time > SETTINGS.IssueLast then
			table.remove(IssueQueue, 1)
		else
			break
		end
	end
end)
