When should you actually use task.(spawn, delay, and defer?)

I was looking through old code and rewriting it and as I was, I wondered if I was using task.spawn in the right context or not, as I noticed I used it frequently for each of my bombs in a module. So, what situations would it be fine to use those parts of the task library?

1 Like

use task.spawn() whenever you want to execute code without blocking the rest of the code
[this isnot a good example]

local a = 0
local isA500 = false
task.spawn(function() 
while true do
if a == 500 then
isA500 = true
else
isA500  = false
end
end)


-- the rest of the code willnot be blocked
2 Likes

How is this even possible when scripts read through each line of code linearly?

And also, when is using the task library too much?

1 Like

when task.Spawn() is called it will execute code in another thread so it will not block the main thread

I don’t see using the task library as a problem as long as its used correctly sometimes you will not need to use the task library and sometimes you will need to

this is a better example
lets say that you want to make a function that will change a text label text from 0 to 10 by 1 every 1 second
– without task.spawn

local function incrementTextLabelText(textLable : TextLabel)
	for i=0, 10 do
		textLable.Text = `{i}`
		task.wait(1)
	end
end

incrementTextLabelText(textLable)

print("Hello") -- willnot run until the loop in the incrementTextLabelText function have finished running

– with task.spawn

local function incrementTextLabelText(textLable : TextLabel)
	for i=0, 10 do
		textLable.Text = `{i}`
		task.wait(1)
	end
end


task.spawn(incrementTextLabelText(textLable))

print("Hello") -- will run without waiting for incrementTextLabelText to finish the loop

or

local function incrementTextLabelText(textLable : TextLabel)
	task.spawn(function()
		for i=0, 10 do
			textLable.Text = `{i}`
			task.wait(1)
		end
	end)
end


incrementTextLabelText()

print("Hello") -- will run without waiting for incrementTextLabelText to finish the loop
2 Likes

Would it be safe to do so inside of a loop for each increment? Or would that be a memory hog?

1 Like

task.spawn() and task.defer() are very similar on the surface level. They both let you run functions without yielding (think task.wait(), event:Wait(), HttpService:RequestAsync(), anything that says “Yields” on the documentation or that can make code pause across multiple frames). The difference between them is that task.spawn() will run the code immediately, while task.defer() will run it a little bit later in the frame.

function hello()
	print("I will print second!") -- This line runs immediately when task.spawn(hello) is called
	task.wait(0.5) -- This yields, so the script engine starts running the next scheduled piece of code
	print("I will print last!")
end

print("I will print first!")
task.spawn(hello)
print("I will print third!") -- This runs once hello() yields, since task.spawn(hello) schedules it to run next
function world()
	print("I will print third!") -- This runs at some point during the same frame task.defer(world) was called
	task.wait(0.5)
	print("I will print last!")
end

print("I will print first!")
task.defer(world)
print("I will print second!") -- This runs before world() does because task.defer(world) schedules it to run later in the frame
task.wait() -- This yields, so the scheduler starts running other scheduled code, which will include world()

As far as I know, task.delay(1,func) is functionally identical to task.defer(function() task.wait(1) func() end). It’s just a bit easier to read. I suppose if you wanted to schedule a function to run next frame, you could use task.delay(0,func) for that.

2 Likes

yes its safe as long as there is an exit or if you will cancel the function [donot make them run forever]

--i wrote this on the web so i was lazy to write them in a for loop but imagine if they are in a for loop
-- not good because it will run forever and the more the function executes the more threads will be running
task.spawn(function()
while true do
task.wait() 
end
)


-- good will stop executing after the loop
task.spawn(function()
for i=0,10 do
task.wait()
end


-- also good
local thread = task.spawn(function()
while true do
task.wait() 
end
)

task.cancel(thread) -- the thread is canceled meaning that it willnot run forever
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.