I am creating a singleton module for the player, where I am planning to make a single connection to execute several functions placed in a table, so that it is not necessary to make multiple connections. Would this be a good way to solve this problem?
Example:
self._internalPlayerConnections.HumanoidDied = self.Humanoid.Died:Connect(function()
task.spawn(function()
for _, Callbacks in self.PlayerConnections.HumanoidDied do
Callbacks()
end
end)
end)
It seems like you’re just reinventing the behavior of the signal anyway. Why not just allow your other scripts to directly connect to the Died signal? You are not going to have performance issues doing that.
(other then the unnecessary task.spawn), it does let you create a sort of “priority” for running cleanup callbacks if desired (table.sort), but other then that using regular connections should work fine.
For your callbacks, task.spawn is good as it’ll run them async, but how you’ve currently set it up they will run in serial. You’d want to task.spawn(Callbacks) instead, so that each callbacks runs independently of the others
As a module idea it could work, but generally those connections are automatically cleaned up on Destroy(). This could simplify cleanup if you have custom connections for stuff that might not be cleaned when the character is destroyed
If you’re worried about performance, you should know it’s faster to use multiple connections instead of one connection running multiple callback functions.