EventEmitter - An alternative to BindableEvents

Hello! I made an event emitter following this JavaScript tutorial. What does it do? You can bind functions to names and call those functions by referencing the name at any time.

API

.new - Creates a new EventEmitter. Takes 0 arguments
:on - Creates a new listener for an event and fires the callback. Takes 2 arguments: event and cb
:emit - Fires the specified event. Takes 1 or more arguments: event and ...
:remove - Removes the specified event. Takes 1 arguments: event

Examples

This creates a function called “test”, fires it, removes it, and attempts to call it again (it will create a warning because it doesn’t exist).

local EventEmitter = require(script.EventEmitter).new()

EventEmitter:on("test", function(data)
	print("test was fired. This data was sent: "..data)
end)

EventEmitter:emit("test", "asdf")
EventEmitter:remove("test")
EventEmitter:emit("test") --warning

This does the same thing, except in minimal lines.

local EventEmitter = require(script.EventEmitter).new()

EventEmitter:on("test", function(data)
	print("test was fired. This data was sent: "..data)
end):emit("test", "asdf"):remove("test"):emit("test")

If you think this was cool or you liked it, thank you. If you don’t, tell me why. Feel free to leave any feedback.

You can download it here: EventEmitter.rbxm (892 Bytes)

2 Likes