How do I make sure to identify the exact same RBXScriptConnection?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I am currently working on a system and I need to keep track of multiple RBXScriptConnection (aka events) related to the player’s current character
function MovementService:AddPlayer(userId:number)
	local listener:userData = self.movementListeners[userId]
	if not listener then
		local character = Players:GetPlayerByUserId(userId).Character or Players:GetPlayerByUserId(userId).CharacterAdded:Wait()
		listener = {Character = character, Connections = {}}
		self.movementListeners[userId] = listener
	end
end
  1. What is the issue? Include screenshots / videos if possible!
    I store a RBXScriptConnection instance in a table. However, when the player dies, I update the table to “link” the new character instead. But, since it’s a new character, I have to update the connection. That means I have to severe it and create a new one.
function MovementService:UpdateCharacter(userId:number)
	local listener:userData? = self.movementListeners[userId]
	if not listener or not listener.Connections then return end
	
	local newCharacter = Players:GetPlayerByUserId(userId).Character or Players:GetPlayerByUserId(userId).CharacterAdded:Wait()
	local existingConnections = self.movementListeners[userId].Connections
	listener = {Character = newCharacter, Connections = existingConnections} --trouble line. ExistingConnections is ofc wrong, just put it here to showcase the trouuble
	
	self.movementListeners[userId] = listener
end

But now the issue: as said, I have to severe the existing connections and make new ones, but since connections do not have any name, I can’t know which one is what and create a new one. Is there a way to identify a connection or to find a connection’s “name” even tho it doesn’t have one?

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Looking online for months now, but I have been asking myself this question for years. And it has finally come to a point where I can’t dodge this issue. I am completely lost :confused:

Make connections a dictionary where the key is the name of the connection and the value is the connection.

1 Like

I thought of that, and wanted to ask here if there wasn’t a more efficient method than creating a 2nd dictionary by any chance? I am trying to lean more into optimization and as a result I am trying to explore new methods (if there exist other methods, by any chance)

You can just have one dictionary by making Connections a dictionary.

Edit: and it’s insanely efficient.

1 Like

Yeah I got that.

My bad I forgot to precise why I’ve asked this question (I edited my original message, but here again:

I am trying to lean more into optimization and as a result I am trying to explore new methods (if there exist other methods, by any chance)

You can store the connections in a dictionary, where the key is the name you give it.

Or you could store them in an array, but you’d have to keep track of which index stores which connection.

1 Like

Thanks for your reply! I haven’t thought about using arrays, but yeah in my scenario it would be a headache since I’ll have a lot of them. Also, since a 2nd developer is also suggesting to use dictionaries,that reassures me :relieved:

I’ll mark @bytesleuth’s answer as the correct one (because they were first), but hopefully both of you will not have helped me today, but also other people in the future :relaxed:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.