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

5 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.

2 Likes

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!

1 Like

Sorry for the late reply, I just woke up.
I will add that disconnection check in v 1.1
When you say ConnectBulk() could you give me a example usage? Like:

ConnectionsModule.ConnectBulk({
[PlayerConnection[player]] = {
[1] —key  = RunService.Heartbeat:Connect(function()
end),
} ,
[GlobalConnections] = {} — add functions
})

Sorry if this wasn’t what you was thinking if it’s not give me example,
I will update the module when i get home from school

1 Like

Alright, and no worries, i had went to bed myself, a use-case for this would be for example:

local module = {}

module.__index = module

local betterConnections = require(script.BetterConnections)

module.connections = {}

module.connections2 = {}

local connections = betterConnections.new()

betterConnections:ConnectBulk(
	{module.connections, module.connections2}, -- connection gets added to these tables and the better connection.
	coroutine.create(function()
		
		while task.wait() do
			print("hello world!")
		end

	end)
)

return module

and if you were to call :Disconnect(), it would disconnect from the other tables too.

I’d suggest keeping track of the tables that the connection is in when :ConnectBulk() is called, so it’s easier to remove them from those tables, hopefully i’ve cleared up the confusion.

My Use-Case (Not Neccessary To Read)


i mainly need this function as my plugin has multiple modules to handle the different aspects of it, and some of the them store there own connections, which get disconnected via a disconnect function in them.

But if the plugin closes before said function executes, memory leak, so i also store them into a global connection table.

so i made a function to connect them to said global connections table and the module’s connections table, but :ConnectBulk() would make it easier.

1 Like

Interesting, I am sick so my brain is just not braining at all.
But I will do the ConnectBulk() so other people can take adavantage of this.
I did make a couple more functions for example I tried doing the ConnectBulk and I made a GlobalConnectBulk that would be used like this:

ConnectionsModule.GlobalConnectBulk({
			[PlayerConnection.identifier] = {
				[1] = RunService.Heartbeat:Connect(function() end),
				[2] = RunService.Heartbeat:Connect(function() end)
			},
			[GlobalConnection.identifier] = {
				["thing"] = RunService.Heartbeat:Connect(function() 
					print(GlobalConnection, PlayerConnection)
				end)
			}
		})

Anyways I will try to replicate what you have done lol.
I will be adding a Identifier variable:
module.new(identifier)
And there is a module.get(name) so you can get connections from different scripts
(they have not been fully tested so I need to test some more)

1 Like

a :GlobalConnectBulk function sounds interesting, also, would the identifier variable be used for?and would module.get() only work with a table created via BetterConnections.new()? just wondering, also, hope you get well.

1 Like

Thanks!
Also all that module.get(i) is everytime you make a module.new("SillyConnections") it stores the metatable in another table in the module then when you use .get("SillyConnections") it just checks if CurrentConnections[Name] exists then returns it, I think its pretty neat.

no problem! and oh ok, so it returns the table it if it exists so i can then, for example, store stuff that table into via another script? if so then that’s cool!

1 Like

Hm, i’ve not actually tested that. But looking at how the function works, I’m pretty sure its a yes.
(ill test rn)

1 Like

Using the .get() function in another script provided with the name it returns this:
image
and you can use functions like self:Connect() etc.

1 Like

alright that’s nice, i’m sure i’ll come across a use-case for this eventually.