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