PausedThread - Pause and resume a delay

https://www.roblox.com/library/9881891628/PausedThread

A simple module to pause and resume a delay, with similar syntax to delay() or task.delay().



Example:

local Thread = PausedThread.new(
	-- Delay time
	5, 
	
	-- Callback function
	function(onePlusOne)
		print('1 + 1 =', onePlusOne);
	end, 

	-- Whether or not to start the thread paused (optional)
	false, 
	
	-- Any arguments passed here will be passed to the callback
	1+1
);

Thread.onPauseChanged:Connect(function(isPaused)
	print('This thread is now:', isPaused and 'Paused' or 'Resumed');
end);

task.wait(1);
Thread:Pause(); -- Delay will pause
task.wait(5);
Thread:Resume(); -- Delay will resume
--Thread:Cancel(); -- Delay will destroy and run callback
--OR
--Thread:Destroy() -- Delay will destroy and NOT callback

Source code available in the module!

Credits

4 Likes

What’s the difference between this and coroutines though

1 Like

This lets you detect when the thread is paused/unpaused with an event, as well as having a lot easier to digest syntax for beginners. If you wanted to acomplish something similar to this module with coroutines, you’d probably just make something like this anyway.

1 Like