How does one pause and resume a thread (task.spawn)

okay
so this has been bothering me for quite a while
ive been trying to figure out how to pause a thread,

task.spawn is able to resume/start threads
but what kind of function makes the thread pause
task.cancel completely mauls & murders the thread, so im not satisfied with that

local Thread = task.spawn(function()
	for i = 1,10,1 do
		warn(i)
		task.wait(1.5)
	end
end)
task.wait(2)
print("pausing thread")
task.cancel(Thread)
task.wait(2)
print("resuming thread")
task.spawn(Thread)

This documentation will be useful for you: coroutine | Documentation - Roblox Creator Hub

coroutine.close() closes threads

afaik you can only suspend the thread inside it and doing it from outside is doable but kinda hacky

whats the use case anyways since threads arent expensive (considering u arent doing dumb shit inside it)

thanks but i still dont get it how do you pause a thread, make it stop and then make it work again

heres the hacky way

local suspend
local thread = task.spawn(function()
    suspend = function()
        coroutine.yield()
    end
	for i = 1,10,1 do
		warn(i)
		task.wait(1.5)
	end
end)
print("pausing thread")
suspend()
task.wait(2)
print("resuming thread")
task.spawn(thread)
1 Like

nope, doesnt work at all

god i hate the character limit

How about this one:


local Stop = false

local thread = coroutine.create(function()
	
	for i=1, 1000 do
		if(Stop) then
			coroutine.yield()
		end
		
		print(i)
		
		task.wait(1)
	end
end)


print("starting thread")

coroutine.resume(thread)



task.wait(3)

Stop = true

print("thread stopped")



task.wait(3)

Stop = false

print("Thread resumed")

coroutine.resume(thread)
1 Like

YEAHHHHHH now this is what i want

1 Like

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