Incorrect order of PostSimulation, Queue Delayed Scripts and Heartbeat

According to documentation scheduler works the wrong way.

Here is a link to documentation: https://create.roblox.com/docs/performance-optimization/microprofiler/task-scheduler

And here is my script:

--!strict


local RunService = game:GetService("RunService")


RunService.PreRender:Connect(function()
	print("PreRender")
	task.defer(function()
		print("Deferred PreRender")
	end)
end)


RunService.PreAnimation:Connect(function()
	print("PreAnimation")
	task.defer(function()
		print("Deferred PreAnimation")
	end)
end)


RunService.PreSimulation:Connect(function()
	print("PreSimulation")
	task.defer(function()
		print("Deferred PreSimulation")
	end)
end)


RunService.PostSimulation:Connect(function()
	print("PostSimulation")
	task.defer(function()
		print("Deffered PostSimulation")
	end)
end)


RunService.Heartbeat:Connect(function()
	print("Heartbeat")
	task.defer(function()
		print("Deferred Heartbeat")
	end)
end)




while true do
	task.wait()
	print("Loop!")
	task.defer(function()
		print("Deferred loop!")
	end)
end

And here is an output:

 22:56:21.670  PreRender  -  Client - LocalScript:8
  22:56:21.670  Deferred PreRender  -  Client - LocalScript:10
  22:56:22.035  PreAnimation  -  Client - LocalScript:16
  22:56:22.036  Deferred PreAnimation  -  Client - LocalScript:18
  22:56:22.036  PreSimulation  -  Client - LocalScript:24
  22:56:22.036  Deferred PreSimulation  -  Client - LocalScript:26
  22:56:22.036  Loop!  -  Client - LocalScript:51
  22:56:22.064  PostSimulation  -  Client - LocalScript:32
  22:56:22.065  Deferred loop!  -  Client - LocalScript:53
  22:56:22.065  Heartbeat  -  Client - LocalScript:40
  22:56:22.065  Deffered PostSimulation  -  Client - LocalScript:34
  22:56:22.065  Deferred Heartbeat  -  Client - LocalScript:42

which totally conflicts with what documentation says if we talk about sections Heartbeat, PostSimulation and Queue Delayed Scripts.

Expected behavior

I expect this behaviour:

PostSimulation → Deferred PostSimulation → Loop (we entered “Queue Delayed Scripts” section) → Deferred Loop! (resume point and the end of “Queue Delayed Scripts” section) → Heartbeat → Deferred Heartbeat

(Tested in Deferred Signal Mode)

Here is a topic where I believe I was pretty close to understanding what exactly happens but I still believe there is a bug here since there are some missing parts such as explanation of why “Deferred” prints have this order.

So I believe there is some bug with task.defer().

Any updates?

Thanks for the report! We’ve assigned this to somebody to take a look at. This indeed seems like a bug.

I’ve just rolled out the fix for this bug. Please let me know if you encounter any further issues :grin: