Is there a way to cancel function task spawns? [Resolved]

I want to figure out a way to cancel a function spawned via “task.spawn”, I’m not really sure how to approach this as “task.cancel(function)” doesn’t appear to work.

ex:

local function LfLegMissingMoveAnims()
	
	while task.wait() do
		
		if Humanoid.MoveDirection ~= Vector3.new( 0, 0, 0) then
			
			LfLegMissingIdle:Stop()
			
			if LfLegMissingTrack.IsPlaying == false then
				LfLegMissingTrack:Play()
			end
		end
		
		
		if Humanoid.MoveDirection == Vector3.new( 0, 0, 0) then
			
			LfLegMissingTrack:Stop()
			
			if LfLegMissingIdle.IsPlaying == false then
				LfLegMissingIdle:Play()
			end
		end
		
		
	end
	
end

task.spawn(LfLegMissingMoveAnims)

--code here to cancel the ongoing task of "LfLegMissingMoveAnims"

I’ve heard that I should use coroutines but I’m not sure how to use/apply them in the same context as the previous code.

task.spawn returns a thread that you can save to a variable and later use task.cancel on.

Example:

local MyTask = task.spawn(MyFunction)
task.cancel(MyTask)
2 Likes

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