➡️RBXConnectionManager – No More Forgotten RBXScriptConnections!

RBXConnectionManager – Simplify RBXScriptConnections!

:link: GitHub Repository: RBXConnectionManager

:package: Wally Package: jarnster/rbxconnectionmanager

:pushpin: Why Use RBXConnectionManager?

Managing event connections in Roblox can quickly become messy when dealing with multiple RBXScriptConnection objects. If you forget to disconnect them, your game can suffer from memory leaks or unexpected behavior.

:rocket: RBXConnectionManager solves this problem by providing a structured way to handle connections, ensuring they are properly stored, monitored, and cleaned up when no longer needed.


:zap: Key Features

:white_check_mark: Easy Connection Management – Store and access connections by name.
:white_check_mark: Automatic Cleanup – Player-specific connections are removed when a player leaves (server-side only).
:white_check_mark: Batch Disconnection – Disconnect all connections or specific groups at once.
:white_check_mark: Event Monitoring – Track event calls with timestamps for debugging.
:white_check_mark: Self-Destruction – Completely clean up the manager when needed.


:inbox_tray: Installation

:one: Manual: Download RBXConnectionManager.luau from GitHub and add it to your Roblox project.

:two: or using Wally: Add to wally.toml:

[dependencies]
rbxconnectionmanager = "jarnster/rbxconnectionmanager@0.1.3"

:three: Require the module where needed:

local RBXConnectionManager = require(path.to.rbxconnectionmanager)

:hammer_and_wrench: How to Use RBXConnectionManager

:small_blue_diamond: Basic Example (Server-side Car Show Handler)

This script demonstrates how to manage event connections in a structured way:
Example: a game with car shows, every player has it’s own car booth and you want to connect an event when you recieve OnCarShowClicked for a specific player (in this case, when the player leaves, you don’t want to listen anymore to the player’s car booth).

local Players = game:GetService("Players")
local RBXConnectionManager = require(game.ServerScriptService.rbxconnectionmanager)

-- Create a new connection manager
local connectionManager = RBXConnectionManager.new()

-- Example RemoteEvent
local remoteEvent = game.ReplicatedStorage.SomeRemoteEvent

-- Connect an event with automatic tracking
Players.PlayerAdded:Connect(function(playerObj)
    local userid = playerObj.UserId
	connectionManager:Connect("OnCarShowClicked_" .. tostring(userid), remoteEvent.OnServerEvent, function(triggeringPlayer, data)
		print(triggeringPlayer.Name .. " triggered the event with data:", data)
        warn("Send " .. triggeringPlayer.Name .. " congratulations about " .. triggeringPlayer.Name .. " clicking on his car show")
	end, true) -- Enable monitoring
end)

In this example, the connection will be automatically destroyed when the player leaves, so the amount of active connections doesn’t keep accumulating and improves performance in situations where you need a lot of script connections.


:stop_sign: Why This is Better Than Manually Managing Connections

:small_blue_diamond: Prevents Memory Leaks – Ensures all connections are properly disconnected.
:small_blue_diamond: Reduces Repetitive Code – No need to manually store and clean up event listeners.
:small_blue_diamond: Improves Debugging – Track which events are firing and when.


:unlock: Open Source & Contributions

RBXConnectionManager is open-source and completely free to use in any Roblox project. Contributions and improvements are welcome!

This is our first open-source release. Please add a :star: on GitHub and :heart: on the DevForum if you like the idea of this project or if you like to use it.

Consider joining our community aswell! Discord - Group Chat That’s All Fun & Games to see progress on our very advanced projects and enjoy early-access benefits while you still can!

Want to have an early look at new features in development? Get a look at the development branch to try it out.

:rocket: Make your connection event handling easier, cleaner, and more efficient with RBXConnectionManager!

12 Likes

Wow this is really nice! I will be using this in my future games.

I do have to ask. How do you detect when a event needs to be disconnected?

1 Like

Thanks! Really glad to hear you’ll be using it in your future games!

To detect when an event should be disconnected, it depends on your use case:

1) When a player leaves the game: All related connections to the player will be cleared automatically. → you need to put the player’s UserId into the event name for that.
For example: "OnCarShowClicked_" .. tostring(playerObj.UserId)

2) You can manually disconnect any event in the connectionManager class:

connectionManager:Disconnect("EventName")

3) You can also disconnect events in the same group:

connectionManager:DisconnectAllInGroup("OnCarShowClicked")

This code snippet will disconnect all events in that contain “OnCarShowClicked”.

Let me know if you need more details or if you have any more questions! I’m happy to help!

I now also updated the post for this. I’m sorry I didn’t explain it correctly in the information of the post.

So this only works in player/local scripts?

No, it also works well in server scripts.

But you said it disconnects when the player leaves. So I can’t use this in an NPC, cause the NPC can’t “leave”.

You can make a custom group for that.

For example:

connectionManager:Connect("OnCharacterLoaded_NPC_" .. tostring(NpcObject.Name), NpcObject.CharacterLoaded, function(character)
    warn("Character loaded", character)
end)

I also use my RBXConnectionManager for an NPC system too.

Then manual disconnect it?

At that point I would just make a list then on script.destroying go through that list and disconnect all.

I do find the sorting pretty nice though

I’ll just have to use it first to see how I feel about it.

1 Like

You can always manually disconnect events, or a whole group with events in it.

This feature that automatically disconnects connections that are no longer needed (when a player leaves) will run automatically if it can. It will still work when it never sees a player or object leaving.

RBXConnectionManager is useful for cases where an NPC system has many duties and can benefit from seperating different groups of connections.

An example snippet of how I use it in my NPC Manager class for playing voicelines:

	self.__connection_manager:Connect("OnVoiceLineEnded", random_voiceline.Ended, function()
		if self._character then
			self.__connection_manager:Disconnect("OnVoiceLineEnded")
			local delay_time = math.random(voicelines_interval_min, voicelines_interval_max)
			task.delay(delay_time, function()
				self:_InitVoiceLines()
			end)
		end
	end)

This helps because you don’t need to manually store a large amount of RBXScriptConnections, but this process is handled more controlled and managed.

1 Like

ooh, a new connections module!! :smiley: I’m very big on preventing memory leaks, so I’ll definitely try using this in one my recent projects, I’ll let u know how it goes.

1 Like

Amazing! Thanks for trying out. Preventing memory leaks is absolutely where we aim for!

I will also be releasing an update that slightly improves memory footprint of the module itself (that can still very slightly increase when you use a lot of RBXConnectionManager instances → but not anymore with the new small update).

Edit: I just released it on GitHub and Wally (v0.1.3).

Also feel free to share your ideas for future additions.

Personally, I’m thinking about a feature that allows you to connect an RBXScriptSignal, when it’s fired to automatically disconnect all events in the group. I already added this feature to the GitHub Development Branch

1 Like

Just use my module man; this is an overcomplicated module for literally nothing.
Here’s mine:

--15.10.2024 | 1Xayd1
local ConnectionManager = {}
ConnectionManager.__index = ConnectionManager

function ConnectionManager.new()
	local self = setmetatable({}, ConnectionManager)
	self.connections = {}
	self.playerConnections = {}
	return self
end

function ConnectionManager:addConnection(event, callback)
	local connection = event:Connect(callback)
	table.insert(self.connections, connection)
end

function ConnectionManager:addPlayerConnection(player, connection)
	self.playerConnections[player] = connection
end

function ConnectionManager:cleanupPlayerConnections(player)
	local connection = self.playerConnections[player]
	if connection then
		connection:Disconnect()
		self.playerConnections[player] = nil
	end
end

function ConnectionManager:disconnectAll()
	for _, connection in ipairs(self.connections) do
		connection:Disconnect()
	end
	self.connections = {}

	for player in pairs(self.playerConnections) do
		self:cleanupPlayerConnections(player)
	end
end

return ConnectionManager

Hey there!

Thanks for sharing your module too! I can see where you’re coming from with the simplicity, and it’s great for quick setups. However, my RBXConnectionManager aims to handle more complex use cases, especially for larger games and systems where you need more structure and monitoring.

I implemented additional features (designed for large-scale projects) like monitoring/debugging, automatic disconnection signals and self-destruction, resulting in the lowest memory footprint.

Additional features like Automatic Disconnection can be found at the development branch.

There are already tons of modules that does exactly this. Maid, Trove, Janitor, you name it. What exactly does this offer that other modules doesn’t have?

3 Likes

For a clicker game, can it handle approx 200 remote firings every second?

1 Like

theyre all the same, some have better effiencey and some are more regularly updated, and some are more easier to understand and use,

2 Likes

Can you make your own thread instead of advertising this? I don’t understand why people feel the need to insert their own modules into other peoples posts.

2 Likes

whats the point of this you can replicate this in like 3 lines of code

You can quickly clear all connections with just one line of code instead of having to copy and paste whenever you want to clear a connection

1 Like

how does this differ from maid/janitor?