Coroutine.yield doesnt work

Hello, i am trying to stop a function from running when an event gets fired.

Heres the code im using:
image
The problem is when i fire the event function does not stop.

Any ideas on how can i fix this issue?

You can get the current thread using coroutine.running(), and you can disable the thread inside the connection using coroutine.close(thread). You’ll have to contain the code inside the function using task.spawn() to work the way you want.

1 Like

Because coroutine.yield() is being called inside the callback thread which it is defaulting to, instead of nonspell1. You’d have to grab the function thread as an upvalue and pass it through.

local RunningThread: thread = coroutine.running()

Event:Connect(function(): ()
	coroutine.yield(RunningThread)
end)

Note the callback thread will still be running even when the function thread is yielded, so you may want to use :Once instead.

Hello.

I want to use this function multiple times, and using coroutine.close() makes the function not able to run again.

You’ll have to contain the code inside the function using task.spawn() to work the way you want

I didn’t quite understand what you meant by this, can you elaborate further? :sweat_smile:

I have tried doing this but it didn’t work for some reason, i can close the thread but not yield it for some reason.