function TaskScheduler.CreateScheduler(H,J)local u={}local G={}local y=true local V local f=nil local S=tick()runService.RenderStepped:Wait()local function R()lastIteration=tick()for J=#frameUpdateTable,1,-1 do frameUpdateTable[J+1]=frameUpdateTable[J]>=lastIteration-1 and frameUpdateTable[J]or nil end frameUpdateTable[1]=lastIteration end local function K()f=runService.RenderStepped:Connect(R)while true do if y then break end local H=tick()-S>=1 and#frameUpdateTable or#frameUpdateTable/(tick()-S)if H>=J and tick()-frameUpdateTable[1]<1/J then if#G>0 then G[1]()table.remove(G,1)else y=true break end else runService.RenderStepped:Wait()end end f:Disconnect()f=nil end function u.Pause(J)V=true y=true end function u.Resume(J)if V then V=false y=false K()end end function u.Destroy(J)u:Pause()for J in pairs(u)do u[J]=nil end setmetatable(u,{__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 u.QueueTask(H,J)G[#G+1]=J if y and not V then y=false K()end end return u 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()
		paused = true
		sleeping = true
	end
	
	function scheduler:Resume()
		if (paused) then
			paused = false
			sleeping = false
			Loop()
		end
	end
	
	function scheduler:Destroy()
		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(callback)
		queue[#queue + 1] = callback 
		if (sleeping and not paused) then
			sleeping = false
			Loop()
		end
	end
	
	return scheduler
	
end


return TaskScheduler
