Is there any risks using this module? Can if be improved? If so, how?

Hello, I was making a “networking” module, and am curious if I did anything bad in it and if it poses any risks

Here is the code for the client, the server is basically the same thing currently

local self = { }
self.Listeners = { }
self.Remotes = { }

local replicatedStorage = game:GetService("ReplicatedStorage")

local function DeleteRemoteEvent(remote: string)
	local proxy = self.Remotes[remote]
	if not proxy then return warn("ERR: Didn't find proxy/remote:", remote) end
	
	proxy.OnDestroy:Disconnect()
	proxy.Connection:Disconnect()
	table.clear(proxy)
	self.Remotes[remote] = nil
end

local function SetupRemoteEvent(remote: RemoteEvent)
	self.Remotes[remote.Name] = {}
	self.Remotes[remote.Name].Remote = remote
	self.Remotes[remote.Name].Connection = remote.OnClientEvent:Connect(function(...)
		local params =  ...
		for _, callback in pairs(self.Remotes[remote.Name]) do
			if type(callback) ~= "function" then continue end
			task.defer(callback, params)
		end
	end)
	
	self.Remotes[remote.Name].OnDestroy = remote.Destroying:Connect(function()
		DeleteRemoteEvent(remote.Name)
	end)
end

local function QueueForRemote(remote: string, attempts: number, timeOut: number)
	if not timeOut then timeOut = 1 end
	print("Queue enabled...")
	
	local proxy = self.Remotes[remote]
	local attempt = 0
	
	while true do
		attempt += 1
		proxy = self.Remotes[remote]
		if attempt > attempts or proxy ~= nil then
			break
		end
		task.wait(timeOut)
	end
	
	return proxy or nil
end

self.ListenToRemote = function(remote: string, callback)
	local proxy = self.Remotes[remote]
	if not proxy then
		proxy = QueueForRemote(remote, 5, 1)
	end
	if not proxy then return warn("ERR: Didn't find proxy/remote:", remote) end
	
	table.insert(proxy, callback)
	return function()
		table.remove(proxy, table.find(proxy, callback))
		print("removed")
	end
end

self.FireServer = function(remote: string, ...)
	local proxy = self.Remotes[remote]
	if not proxy then return warn("ERR: Didn't find proxy/remote:", remote) end
	
	proxy.Remote:FireServer(...)
end

do -- init
	for _, remote in ipairs(replicatedStorage:WaitForChild("Remotes"):GetChildren()) do
		if remote:IsA("RemoteEvent") then
			task.spawn(SetupRemoteEvent, remote)
		elseif remote:IsA("RemoteFunction") then
			task.spawn(function()
				
			end)
		end
	end

	replicatedStorage.Remotes.ChildAdded:Connect(function(remote)
		if remote:IsA("RemoteEvent") then
			task.spawn(SetupRemoteEvent, remote)
		end
	end)
end

return self
1 Like