Cancel a wait/function if something happens?

current code:

mouse.Button2Down:Connect(function()
	task.spawn(function()
		

	held = true
			anim2:Play()
			task.wait(anim2.Length * 0.90)
			anim2:AdjustSpeed(0)
			anim2.TimePosition = anim2.Length - .000001	
		end)
	end)
	mouse.Button2Up:Connect(function()
		held = false
			anim2:Stop()
	end

so my problem is i am trying to stop an animation at the last frame, however, the animation can stop before the wait finishes. i am wondering how i can maybe stop waiting or break the wait somehow?
i tried using

	local x = 0
	repeat wait(0.1)  x = x+0.1 until x == 1.2 or held == false 

but with the same result, idk if my logic is wrong or something else. Please help, thx

You are on the right path, I actually don’t know why your solution doesn’t work, it seems to me like it should work. Maybe some weird stuff happening with the or at the end, although that would be very surprising to me

Here is how I would write it

local EndTime = os.clock() + anim2.Length * 0.90

while held and os.clock() < EndTime do -- While help isn't nil/false, and the wait time isn't reached, the loop keeps running
   task.wait()
end

this would probably work, how ever, the thing would still play, beacuse the player could tap first and hold immedietly after, which causes two waits, the first held statement will pass because of the second holding, which causes both to run

terrible drawing:


i am currently trying task.cancel, but still doesnt work for some reason

mouse.Button2Down:Connect(function()
	local func = task.spawn(tasker)
		
	mouse.Button2Up:Connect(function()
		held = false
		if func ~= nil then
			task.cancel(func)
			end
	end

(tasker is just the anim function)

you should define held as a local variable inside your function then, to ensure that another tap doesn’t change the held variable of the first tap

task.cancel is not appropriate here, I don’t think it can be used to cancel task.wait, as that isn’t a thread…? Unless you can get the thread before the task.wait() using coroutine.running(), if the thread doesn’t change with the task.wait()

However, there is this example I found in the docs that would actually work in your situation

sorry if i was being confusing, i am using task.spawn (as you can see) to cancel the entire function
ill try task.delay and let you know

1 Like

i think task.delay would delay the entire function, do i put the task.delay inside the function?

What you should do is simply put anim2:Play() before the task.delay, and then put the other stuff inside the task.delay

it would look like this

	held = true
	anim2:Play()

	local thread = task.delay(anim2.Length * 0.90,function()
		anim2:AdjustSpeed(0)
		anim2.TimePosition = anim2.Length - .000001	
	end)

I just thought about a possible issue, I wonder what happens if you use task.cancel on a thread that has already ran. It might error when the animation played fully and ended, before task.cancel is called

i think it worked, if it didn’t the timing is too hard to do (not a bug if its hard to do:wink: ) i think ill call it fixed, thx

yeah i encountered that, it turns nil when it already ran, i just put an if statement to check for nils

1 Like

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