Practical use of task.defer?

Where would the defer function of the task library would be usable in any scenario whatsoever under any circumstance? Can you provide examples of the same? Why does this function exist in the first place?

1 Like

I would recommend to use this in most cases, as pien said, it functions similarly in a sense that it creates a new thread, the difference is when it creates itā€¦ Idk if you know have the task schedular works, but esstainly each frame has a list of task it needs to do like rendering or physics calculations. Task.spawn creates a task as soon as possible which usually means it creates it during the same frame, this can delay other task from running first like physics for example. task.defer however creates the task usually in the next frame to avoid it from delaying task in the current frame

In most cases where you need to create another thread its often not important enough to justify the immediate creation of it.

Keep in mind that task.spawn does not gunaturee code running at the same time, even though its immediate creating 2 task.spawns and running code in both of them does not mean it will run code at the same time

In short task.defer is mostly preferred over task.spawn for performance reasons, and I would recommand the usage of defer over spawn in most cases

NOTES: The way the task schedular works is poorly documented and most of this explaination comes from purely specutlation, experience and talking to other developers, I would not treat this explaination as 100% fact, nor am I a expert in how roblox works.

20 Likes

An example from the Task Library Announcement:

13 Likes

Thanks for the response, now however, my question has inverted. Are there any situations where you need to spawn code instantly rather than waiting for the task scheduler to finish for the frame? What benefits could it provide to instantly run code?

1 Like

Thanks for the reference, the reply above that one helped.

Personally I never had the need to justify the creation of a instant thread, if I did then there usually other better methodsā€¦ The only one I can think of on the top of my head is running some sort of data store call when the player joins the game like so

local function NewPlayer(Player)
    -- Initialize Data or Perform datastore calls
end

Players.PlayerAdded:Connect(NewPlayer)
for _,Player in ipairs(Players:GetPlayers()) do
    task.spawn(NewPlayer,Player)
end

In my experience this has been the only justification that I can find reasonable to use task.spawn over task.defer since you want the data for the player to be intiailized as quickly as possible in a lot of cases

2 Likes

Ah thanks, would physics calculations fit in this category?

Generally speaking no, since physics should be calculated in 1 thread and not in multiple threads

Example:

game:GetService("RunService").Stepped:Connect(function()
    for _,PhysicsObject in ipairs(PhysicsObjects) do
       -- physics calculation
    end
end)
3 Likes

I can give an example of a scenario where defer is best to use!

Letā€™s say youā€™re making a game pass called ā€œVIPā€, and the player just purchased it from your game. This VIP gives the player a special text color and tag in the chat, along with some other things like double coins. You could defer the function that gives the player the special text and double coins because itā€™s ok if it takes a second for the effect of them buying it to take place. There are a bunch of other examples of when to use defer like this.

Hope this helps!

2 Likes

Iā€™ve found that when having to change a value around the same frame, task.defer is needed to make sure that any events triggered by the variable completes before changin the value again.

This ensures that your variable changes when needed, without delaying other code.

		variable = true

		task.defer(function()
			variable = false
		end)
2 Likes