Have you ever made an object-oriented module, and thought: “Hmmm, an event or two would be really nice to make this module easier to use.”
Well, worry not! For I have completed my NetSignal Event Creator module which can do exactly that!
Linkie:
You may be wondering: “But Athar, other people have made this!” This is true.
But one problem i’ve noticed is that event signals do not replicate across the client-server boundary, which is a huge problem when you’re trying to make it so that events fire for both server and client!
Usually, the only way to fix this is to fire the event both on the server and client seperately.
But, with NetSignal, there is a new method called :Remote(…) which does cross the client-server boundary!
Another problem ive noticed with other modules is that they have unfamiliar syntax which is unusual to get used to.
Well, worry not! NetSignal has custom function autocomplete and also extremely familiar syntax, almost exactly the same as vanilla Roblox RBXScriptSignals.
-
CONSTRUCTOR
Now, you may be wondering, “How do I create a new Event using this module?” Well, first of all you need ANOTHER module. This is because NetSignal is only to be used within other modules.
Once you have the module you want to create an event for, the next step is just to call :Create(…) and set the returned Event as a variable in your module.
The first argument of :Create(…) is simply just the name of your event. This is a string.
(Note that this name cannot be randomly generated every time, as both the Server and Client need the correct name as to be synchronous.)
The second argument is the type of function you want to set as the first argument of :Connect(func) for that event. This is to aid autocomplete with the parameters of your event.
The third argument is the type of function you want to set as the :Fire(…) function for your event. This is to aid autocomplete with the parameters of your event when you call :Fire(…). It is simply the second argument with an additional ‘self’ parameter.
The fourth argument is additionalInfo, where you specify any additional options youd like to configure within the event, such as useBuffers, sendTick and maxRequests.
CODE SAMPLE:
local netSignal = require(NetSignalPath) -- pseudocode myModule = {} myModule.Event = netSignal:Create( "MyEvent" function( Foo: number, Bar: string) end function( self: netSignal.RBXScriptSignal, Foo: number, Bar: string) end { maxRequests = 5 useBuffers = true sendTick = false } ) return myModule
And we’re done! This is all you need to do to create a new Event using NetSignal.
Now let’s discuss all the methods that you can call on this Event!
-
METHODS
-
Event:Connect(func)
Have you ever used :Connect(func) on a regular Roblox event, such as BasePart.Touched:Connect(function(HitPart))? Well, this is literally the same thing. Any functions you connect to this event will activate when Event:Fire(…) or Event:Remote(…) is called.
CODE SAMPLE (using the previous myModule module):
local myModule = require(MyModulePath) --pseudocode myModule.Event:Connect(function(Foo, Bar) -- number, string print(Foo, Bar) end
-
Event:Once(func)
Essentially the same thing as Event:Connect(func), but after the Event is fired once this connection is instantly disconnected.
-
Event:Wait(timeOut: number?)
This method yields the current thread until :Fire(…) or :Remote(…) is called, then it resumes the thread and returns all of the passed arguments as a tuple. Similarly to RBXScriptSignal:Wait().
If the timeOut parameter is a number then it will wait that amount of seconds, and if a :Fire(…) or :Remote(…) call is not done within that time frame it will resume the thread anyway and return nil.
-
Event:Fire(…)
This method calls all connected funcs of the event within the same client/server, passing the arguments as a tuple.
CODE SAMPLE (using the previous myModule module.);
local myModule = require(MyModulePath) --pseudocode myModule:Fire(10, "Bar")
-
Event:Remote(…)
Now the moment you’ve all been waiting for, the Remote method! This essentially does the same thing as :Fire(…), but instead of firing all connected funcs on the same client/server, it instead fires on the opposite side of the client-server boundary.
For example, if you called :Remote(Player, args) on the server, it would fire all connected funcs of that specific event on the specified Client.
If you instead called :Remote(args) on the server, then it would fire all connected funcs of that specific event on all clients,
The same thing on the client. For example, calling :Remote(args) on the client would fire all connected funcs of that specific event on the server.
CODE SAMPLE (using the previous myModule module):
-- Server local myModule = require(MyModulePath) --pseudocode while task.wait(0.1) do myModule.Event:Remote(10, "Bar") end
-- Client local myModule = require(MyModulePath) --pseudocode myModule.Event:Connect(function(Foo, Bar) print(Foo, Bar) -- 10, Bar end
Important to mention that if you call :Remote(…) on the client, it has a rate limit. You can turn this off by setting the maxRequests argument in the additionalInfo argument of :Create(…) to -1, but you can set it to any positive integer.
-
You also unfortunately cannot send Instances through :Remote(…) yet, but I will be adding a serializer for that soon.
By now you may be wondering how performant the :Remote(…) method is, since it uses buffers it’s actually pretty performant!
I performed a test where every 100 milliseconds the server calls :Remote(math.random(0, 123182), math.random(0, 1293192)), and the Client performs a square root operation on the first and second variable, then prints it, everytime a signal is fired from the server.
The result was an average of around 30 fps, which is pretty good (I think)!
Anyways, that’s basically it! Enjoy! (please report any bugs here, ty!)
- Did you find this useful?
- Yes
- No
0 voters