Remote Events in Multiple Scripts

Can the same remote event be handled in more than one script, or does the remote event somehow become consumed?

For example,

  1. Can I have two different local scripts handle the same remote event via OnClientEvent. (Local script 1 will do one thing when it receives the event, and local script 2 will do another thing.)

  2. Can I have two different server scripts handle the same remote event via OnServerEvent.

1 Like

You could do a simple test to check it out, but to quickly answer your question. Yes, they can.

Just like all events (.Touched, .Changed), they can be used multiple times across different scripts and it will still activate in all scripts. Think, in real life, if you have someone shouting (an event) and two people listening for the shouting, both of them will hear the shout (the connection).

A remote function does not make sense to act in the same way. They, unlike the procedural way events work, are meant to return something. If you have multiple scripts which will carry out a function but they both return different values, you can see it would get very confusing. The real life analogy breaks down here, I couldn’t think of any way it would actually make sense. Basically, there can only be one function as otherwise it will receive more than one value because there’s no way of preventing it, just doing return returns nil.

4 Likes

RemoteEvents, yes, meaning you can make very clean Unix way scripts.

RemoteFunctions, however, no.

2 Likes

Short answer:
Yes.

Long answer:
Because you listen to an event when using OnServerEvent/OnClientEvent it’s possible since it returns a RBXScriptSignal each time, and when that event gets fired, it fires all listeners connected to the event. There shouldn’t be an issue when creating extra listeners for events. If you want more info about RBXScriptSignal there’s a neat article for it on the wiki which is pretty helpful.

Also, as General, and Kamp said this won’t work for remote functions since they usually return a value before the other listener does, at least that’s what I suppose.

5 Likes

Unless you don’t mind missing fired signals, then you should only have at most one script handing a RemoteEvent.

For example,

  1. One script connects to OnClientEvent. This causes all queued events to fire immediately.
  2. Another script connects to OnClientEvent. It misses out on the queued events, because they were already consumed by the first script.

Note that this isn’t a problem with signals in general. It’s only an issue for RemoteEvents in particular because the On(Client/Server)Event signal is fired as a response to connecting to the signal.

3 Likes

In case it wasn’t mentioned above, you can invoke remote functions from multiple localScripts, but the response callback should be handled on serverside with one script.

1 Like