function TaskScheduler.CreateScheduler(Z,a)local T={}local D={}local U=true local L local h=nil local g=tick()runService.RenderStepped:Wait()local function V()lastIteration=tick()for a=#frameUpdateTable,1,-1 do frameUpdateTable[a+1]=frameUpdateTable[a]>=lastIteration-1 and frameUpdateTable[a]or nil end frameUpdateTable[1]=lastIteration end local function z()h=runService.RenderStepped:Connect(V)while true do if U then break end local Z=tick()-g>=1 and#frameUpdateTable or#frameUpdateTable/(tick()-g)if Z>=a and tick()-frameUpdateTable[1]<1/a then if#D>0 then D[1]()table.remove(D,1)else U=true break end else runService.RenderStepped:Wait()end end h:Disconnect()h=nil end function T.Pause(a)L=true U=true end function T.Resume(a)if L then L=false U=false z()end end function T.Destroy(a)T:Pause()for a in pairs(T)do T[a]=nil end setmetatable(T,{__index=function()error("\065\116\116\101\109\112\116 \116\111 \117\115\101 \100\101\115\116\114\111\121\101\100 \115\099\104\101\100\117\108\101\114")end;__newindex=function()error("\065\116\116\101\109\112\116 \116\111 \117\115\101 \100\101\115\116\114\111\121\101\100 \115\099\104\101\100\117\108\101\114")end})end function T.QueueTask(a,Z)D[#D+1]=Z if U and not L then U=false z()end end return T end return TaskScheduler



function TaskScheduler:CreateScheduler(targetFps)
	
	local scheduler = {}
	local queue = {}
	local sleeping = true
	local paused
	
	local updateFrameTableEvent = nil
	
	local start = tick()
	runService.RenderStepped:Wait()
	
	local function UpdateFrameTable()
		lastIteration = tick()
		for i = #frameUpdateTable,1,-1 do
			frameUpdateTable[i + 1] = ((frameUpdateTable[i] >= (lastIteration - 1)) and frameUpdateTable[i] or nil)
		end
		frameUpdateTable[1] = lastIteration
	end

	local function Loop()
		updateFrameTableEvent = runService.RenderStepped:Connect(UpdateFrameTable)
		while (true) do
			if (sleeping) then break end
			local fps = (((tick() - start) >= 1 and #frameUpdateTable) or (#frameUpdateTable / (tick() - start)))
			if (fps >= targetFps and (tick() - frameUpdateTable[1]) < (1 / targetFps)) then
				if (#queue > 0) then
					queue[1]()
					table.remove(queue, 1)
				else
					sleeping = true
					break
				end
			else
				runService.RenderStepped:Wait()
			end
		end
		updateFrameTableEvent:Disconnect()
		updateFrameTableEvent = nil
	end

	function scheduler.Pause(_s)
		paused = true
		sleeping = true
	end
	
	function scheduler.Resume(_s)
		if (paused) then
			paused = false
			sleeping = false
			Loop()
		end
	end
	
	function scheduler.Destroy(_s)
		scheduler:Pause()
		for i in pairs(scheduler) do
			scheduler[i] = nil
		end
		setmetatable(scheduler, {
			__index = function()
				error("Attempt to use destroyed scheduler")
			end;
			__newindex = function()
				error("Attempt to use destroyed scheduler")
			end;
		})
	end
	
	function scheduler.QueueTask(_s, callback)
		queue[#queue + 1] = callback
		if (sleeping and not paused) then
			sleeping = false
			Loop()
		end
	end
	
	return scheduler
	
end


return TaskScheduler
