Multiple Event Listeners within same script

Hi Everyone.

Are there any negatives to housing multiple event listeners within the same script? Here is an example of the code I would be writing on the client side:

event1.OnClientEvent:Connect(mainFunction) – watch to see if event1 fires
event2.OnClientEvent:Connect(mainFunction) – watch to see if event2 fires
event3.OnClientEvent:Connect(mainFunction) – watch to see if event 3 fires

Connecting a function to an event doesn’t do anything inside your script. It just takes the block of code (the function) and runs it every time the event happens.

Setting up multiple Events in one place is not inherently bad, But why do you have them all running the same function?

It is a function that displays a GUI. The contents of the GUI are dynamic based on what each event is passing to it from the server.

Just wasn’t sure if that is good practice or not. It seems simpler and it works, but not sure if I’m creating bigger problems or not.

You could use one Display Event and use passed info for all the variability but it shouldn’t mess up something if you leave it as is. Then it would pair well with your single function.

Essentially the event would be saying to the client to run mainFunction.

Ok, so it’s fine to use a single remote event by separate server functions?

Thanks for your help.

Yep Events are just game announcements. Anyone (various scripts) can listen to it and do what they feel is a good response to it.
And different things(also scripts) can send out the same kind of announcement(single event). The details (parameters) being slightly different.

So, perhaps one more question, and thank you again for the help and clarification.

What about from an organization perspective? Would it be better to organize “events” that can happen with separate remote events or just lump everything into a single remote event as a catch all? Maybe that is just programmer preference?

Preference, I have seen people try to use a single event for just about everything.

I like to use one for each notable event so it can be named appropriately. Feels more true to the event driven architecture.

2 Likes