Custom Rbx script Signals

Hi im making a .Changed event for table that listens for changes then shows the new value like .Changed does

function d.Changed(dict)
	local ce  =  event.new('.Changed', function(nv)
		print(nv)
	end)
	
	local listen = internal.listenToDeepChanges(dict, function(updatedtable, key, value)
		-- Table has changed
		ce:fire(value)
	end)
	
	return ce
end

This is the function in my table i want it to be used like this

local dict = {}

local module = require(....)

module.Changed(dict):Connect(function(newvalue)

end)

The outside modules i used can be found here:

Listen for changes
function module.listenToDeepChanges(t, onChange)
    local mt = {
        __index = t,
        __newindex = function (self, k, v)
            if type(v) == "table" then
                rawset(t, k, module.listenToDeepChanges(v, onChange))
            else
                rawset(t, k, v)
            end
            onChange(t, k, v)
        end
    }
    for k, v in pairs(t) do
        if type(v) == "table" then
            t[k] = module.listenToDeepChanges(v, onChange)
        end
    end
    return setmetatable({}, mt)
end

Event Module
local Connection = {}
Connection.__index = Connection

function Connection.new(signal, func)
	local self = {
		signal = signal,
		func = func,
	}
	setmetatable(self, Connection)

	return self
end

function Connection:disconnect()
	if self.signal then
		self.signal.connections[self.func] = nil
		self.signal = nil
		self.func = nil
	end
end

local Signal = {}
Signal.__index = Signal

function Signal.new()
	local self = {
		connections = {},
	}
	setmetatable(self, Signal)

	return self
end

function Signal:fire(...)
	for func,_ in pairs(self.connections) do
		func(...)
	end
end

function Signal:connect(func)
	self.connections[func] = true
	return Connection.new(self, func)
end

return Signal

Can anyone help me create something of this sort because posts that i found were years old and i don’t want to bump it up. Thanks!

2 Likes