local module = { Listen = {}, _prototype = {}}
local callback
function module.Listen:Connect(...)
callback = ...
return module._prototype
end
function module._prototype:Disconnect()
--self = nil
--callback = nil
end
spawn(function()
while wait(1) do
callback()
end
end)
return module
The problem with this, is that you need to connect/create the function immediately after you required the module, else it would error.
local module = require(...)
local v = module.Listen:Connect(function()
print("called")
end)
wait(5)
v:Disconnect()
Also for the Disconnect, if I just set self to nil nothing happens, but if I set the callback to nil, it just gives me an error, or I could just use an if statement if callback is not a nil on the for loop.
I managed to make the :Disconnect() work, though I’m not sure if this is the best way of doing it.
local module = { Listen = {}, _prototype = {}}
function module.Listen:Connect(func : (any)) : RBXScriptConnection
table.insert(module, true)
local index = #module
module[index] = func
local prototype = {}
function prototype:Disconnect()
local self : RBXScriptConnection = self
module[index] = nil
end
end
spawn(function()
while wait(1) do
for _, value in ipairs(module) do
value()
end
end
end)
return module
local test = require(...)
local a = test.Listen:Connect(function()
print("Called")
end)
local b = test.Listen:Connect(function() end)
local c = test.Listen:Connect(function() end)
local d = test.Listen:Connect(function() end)
wait(3)
a:Disconnect()
b:Disconnect()
c:Disconnect()
d:Disconnect()
I use this simple signal module to do my fake connections. I just wrote it so it’s clean and fresh for edits.
This is a good way for creating fake signals and I tend to do that for my naming conventions.
local signal = {}
function signal.new()
local self = {}
local bindableEvent = Instance.new("BindableEvent")
function self:connect(...)
return bindableEvent.Event:Connect(...)
end
function self:wait(...)
return bindableEvent.Event:Wait(...)
end
function self:fire(...)
return bindableEvent:Fire(...)
end
return self
end
return signal
I already knew about bindableEvents, I don’t know why but I don’t like using it, probably because I wanna learn how the bindable event was made or I just wanna make my own connections, with module script.
BindableEvent inherits Instance. It’s just a simple userdata object with a metatable attached. The said metatable contains identifier __type to determine that it’s an Instance. Other than that, there’s not much to it, an Instance is not much different from a basic userdata.