I’ve recently made a Signal module that allows use of events in code rather than objects.
Let’s take an added event for example, we want to detect when a new table is added.
In this case, it would revolve around something like:
module.NewTableAdded:Connect(function(args)
print("This was fired!")
end)
and on the module side:
module.NewTableAdded = Signal.new()
-- when a table is added
module.NewTableAdded:Fire("Test")
In a sense, it fires a bindable event with any arguments given, and the other script listens for those events.
Does there need to be any changes in the source? (performance, deprecation, organization)
local Signal = {}
Signal.__index = Signal
function Signal.new()
local bindable = Instance.new("BindableEvent")
local self = setmetatable({}, Signal)
self._event = bindable
self._fired = false
self._signals = {}
return self
end
function Signal:Fire()
return self._event:Fire()
end
function Signal:Connect(func)
table.insert(self._signals, self._event.Event:Connect(func))
end
function Signal:Wait()
return self._event.Event:Wait()
end
function Signal:Destroy()
self._event:Destroy()
for i, v in pairs(self._signals) do
v:Disconnect()
self._signals[i] = nil
end
end
return Signal