C# Events ported to ROBLOX

This module is an extension of my previous delegate module

With delegates, any script can call the delegate. This isn’t good because your code can be very exploitable and vulnerable, however with events only scripts with access to the event “key” can fire it


:warning: Important stuff :warning:

This module does NOT and never WILL cross the network boundary because if I do I’d probably end up creating an entire framework which I don’t wanna do
and it also hurts my head


How to use

Require the module

local eventClass = require (...)

and instantiate a new event

-- The "key" part is important
-- do not exclude it or it would be useless
local event, key = eventClass.new ()

don’t lose the key or you can’t fire the event!
Add as many functions as you need to


function Function1 (message : string)
	print ("Function 1 says ", message)
end

function Function2 (message : string)
	print ("Function 2 says ", message)
end

function Function3 (message : string)
	print ("Function 3 says ", message)
end

function Function4 (message : string)
	print ("Function 4 says ", message)
end

local event, key = eventClass.new (
	Function1,
	Function2,
	Function3,
	Function4
)

-- You can also do it like this
--local event, key = eventClass.new (
--	function ()
-- 		...	
--  end,

--	function ()
-- 		...	
--  end,

--	function ()
-- 		...	
--  end,

--	function ()
-- 		...	
--  end,
--)

Then call the event with the key as the first parameter, then add any other parameters your functions need

event (key, "something")

You need the key or it won’t work
image

You can also remove functions by doing subtracting the event from the function


Installation

create.roblox.com/store/asset/135593838108885


5 Likes

How is this any different than using a Signal module? Still cool concept.

Yes, an event can be called by any script if you make that event publicly accessible. Instead of requiring a key to fire the event, why don’t you make the event inaccessible to random other scripts? If you want scripts to be able to connect but not be able to fire, you can expose a method to do this.

-- psuedo code
local privateEvent: RBXScriptSignal

return function(callback): RBXScriptConnection
    return privateEvent:Connect(callback)
end

most signal modules allow any script to fire the signal this one is different

That works too but this is just a different way of doing that