BetterConnectionsV2 - A workaround for connections(?)

I made a BetterConnections module yesterday, I looked it at and though to myself…
I can do better than this. So I remade the module with some more functions and a more optimized script

What’s the difference between V1 and V2?
In V2 I removed Trove because I don’t really think its necessary,
more functions like: self:CheckConnection(Key) and self:DisconnectBulk({Keys}),
more readability in the module and simpler paramaters

EXAMPLE SERVER SCRIPT:

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ConnectionsModule = require(ReplicatedStorage:FindFirstChild("BetterConnections")) --path for module
local PlayerConnections = {}

function OnPlayerAdded(Player: Player)
	local PlayerConnection = ConnectionsModule.new()
	if PlayerConnection then -- checks
		PlayerConnections[Player] = PlayerConnection
		
		PlayerConnection:Connect(1, RunService.Heartbeat:Connect(function() -- Connects a Connection
			print("Player is still here!")
		end))
		PlayerConnection:Connect(2, RunService.Heartbeat:Connect(function() -- Connects a Connection
			print("This is just another connection")
		end))
		
		task.wait(5)
		PlayerConnection:DisconnectBulk({ -- disconnects multiple connections at once (insert keys)
			1,
			2
		})
		
		PlayerConnection:Destroy() -- destroys
	end
end

function OnPlayerRemoving(Player: Player)
	if PlayerConnections[Player] then
		PlayerConnections[Player]:Destroy()
		PlayerConnections[Player] = nil
	end
end

Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoving)

FUNCTIONS:

module.new()
makes the connection
--------
self:Connect(Key, Function)
Example:

Connections:Connect(1, RunService.Heartbeat:Connect(function(DeltaTime) 
print(DeltaTime) 
end))

--------
self:Disconnect(Key)
Example:

Connections:Disconnect(1)

--------
self:DisconnectBulk({Keys})
Example:

Connections:DisconnectBulk({
1,
2
})

reminder make sure it is a table
--------
self:DisconnectAll()
disconnects all current functions
--------
self:DebrisConnection(Key, Duration, Function)
Example:

Connections:DebrisConnection(1, 3, RunService.Heartbeat:Connect(function(DeltaTime) 
print(DeltaTime) 
end))

--------
self:ChangeConnection(KeyToChange, NewFunction)
Example:

PlayerConnection:ChangeConnection(2, RunService.Heartbeat:Connect(function()
	print("I changed this connection")
end))

--------
self:CheckConnection(Key)
checks if there is a connection and returns true or false
--------
self:GetAllConnections()
returns the table of connections
--------
self:Destroy()
cleans up everything
------------------

Why use this over roblox connections?

Personally I think its better I like my connections being stored in a table where there is some pretty neat functions, so its just down to opinion.
--------

Roblox Model:

RBXM File:
BetterConnectionsV2.rbxm (1.5 KB)

IF YOU HAVE ANY ISSUES OR BUGS WITH THE MODULE DM ME OR ASK HERE

2 Likes

can’t wait to implement this into my plugin! btw, the plugin link leads to configure page for your module, and, you could’ve posted this updated version using your original post, but it’s not that big of a deal.

1 Like

Haha thanks and another thanks for reminding me. The only reason I made another post is because I remade the whole module from scratch and I didn’t want to remove V1 just incase other people prefer it, anyways this will be the last post (hopefully lol), all updates here.

1 Like

your welcome, and yeah that makes sense, btw, instead of using the dashes as the dividers you can type <hr> to create one, also why is ChangeConnection labeled as buggy? sorry if i’m asking alot of questions just wanna make sure everything is clean and working lol :coefficients:

1 Like

I was actually testing ChangeConnection() as we speak and it seems to work as intended now ill just remove that. (I only put it because I was having issues with it like very early on)
Sorry about that also thanks for the tip!
(Whats this plugin by the way im just curious lol)

oops I replied to a quote

1 Like

oh ok, and no problem! the plugin is heavily inspired by F3X, but it’ll have smoother animations and (hopefully) better functionality, i’m mostly making it because F3X is kinda outdated and i wanted certain features to be added to it, i’ll most likely make a Creations Feedback post about it once it’s stable enough.

1 Like

Ah that’s sounds promising, I wish you luck on that and if there’s any issues you come across with the module just tell me.

1 Like

thanks! and i will. (charlimit)

1 Like

Have you seen Maid by Quenty? Maid is more generalized, has more functionality, and has been used by many for years. I would consider using that over this.

1 Like

I totally read that wrong :man_facepalming:
Anyways, I prefer this over Maid just because its easier to understand and has unique functions.
It may be a bit more complex but I think its worth it.
Like I said its opinion based so I respect it wether the less.

added a simple if statement in the :Disconnect function to check if the connection is a thread (which are usually created by using coroutine.create or task.spawn.)

function Connections:Disconnect(Key)
	if self.Connections and self:_checkConnection(Key) then
		if typeof(self.Connections[Key]) == "RBXScriptConnection" then
			self.Connections[Key]:Disconnect()
		elseif typeof(self.Connections[Key]) == "thread" then
			task.cancel(self.Connections[Key])
		end
		if self.Connections[Key] then
			self.Connections[Key] = nil
		end
		self.Events.Disconnected(tostring(Key))
	end
end

also, could you make a :ConnectBulk function, so you can add a function to multiple tables (which the tables don’t have to be a BetterConnection.) that’d be great!