RBXScriptSignal optimization help

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.

2 Likes

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

1 Like

Hey just one note about your example

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

1 Like

If you’re worried about performance, you should know it’s faster to use multiple connections instead of one connection running multiple callback functions.

1 Like

Could you explain to me why it would be unnecessary?

Oh. I thought it would be more efficient than just making multiple connections. Thank you for letting me know.