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
mouse.Button2Down:Connect(function()
local func = task.spawn(tasker)
mouse.Button2Up:Connect(function()
held = false
if func ~= nil then
task.cancel(func)
end
end
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
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