[Open-Source]➡️RBXConnectionManager – The Easiest Way to Manage🔗RBXScriptConnections

:fire: RBXConnectionManager – Simplify Event Handling!

:link: GitHub Repository: RBXConnectionManager

:package: Wally Package: jarnster/rbxconnectionmanager

:pushpin: Why Use RBXConnectionManager?

Managing connection events 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.lua 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!

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

3 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).

More additional features coming soon! Also feel free to share your ideas for future additions.

1 Like