Coroutines vs BindableEvents

I’m conflicted on whether I should use Coroutines or BindableEvents for starting new threads.

Coroutines:

local func = coroutine.create(function()
    print("Coroutine")
end)

coroutine.resume(func)

BindableEvents

local bindableEvent = Instance.new("BindableEvent")
bindableEvent.Event:Connect(function()
    print("BindableEvent")
end)

bindableEvent:Fire()

Are there any benefits in using one over the other, or is it just up to personal preference? Which one do you use, and why?

1 Like

It depends. I personally use bindables. With coroutine.create, any exceptions need to be handled (it’s like pcall where it stops exception from propagating and returns the success and message), where as bindable events don’t have this. Also coroutines kill the stack trace so rip debugging.

btw coroutine.create returns a new thread so naming the variable func is misleading :stuck_out_tongue: (though coroutine.wrap does return a function)

2 Likes

There are a few threads that I found related to your inquiry

5 Likes

Thanks for the quick reply! I’m starting to lean towards BindableEvents, since I don’t want to make debugging harder for myself.
And yeah, I forgot coroutine.create() returns a new thread, not a function! :sweat_smile:

1 Like

Personally I would use coroutines, as using bindable events just seem like a hacky workaround. I feel like coroutines would be faster, as you don’t have to ping every script and check if it has a listener. However, I’m not sure how either does garbage collection but I think events are automated and coroutines aren’t.

1 Like

Bindable events are somewhat hacky, yes, but at least debugging is less of a pain with bindables.

2 Likes

True. Even though I’m all for team coroutines BindableEvents are much easier to debug.

2 Likes

I usually only ever use bindable events if it is something that multiple scripts / threads / etc are connecting to. I also do not use coroutines that often because I try to keep things orderly in a sequential manner. Also, coroutine.wrap seems to have proper error logging unlike with resume(create)

1 Like

I’ve been in a big coroutine mood lately, so I would recommend those. I find them very useful for centralizing things and utilizing multi-threading without creating a separate script.

I would use BindableEvent, like @Wunder_Wulfe say, to connect multiple scripts, with RemoteEvents you only can have a client to server or server to client connection, but with BindableEvent you can make your own events and have a Server to server script connection. Coroutine.wrap is more if you have two loops that you want to play in the same moment, and i think that it should play your function faster.

coroutine.wrap() does not act like a pcall(), if an error is raised, it will be an error in the output, so it would probably be better using coroutine.wrap()