Does an attribute connection disconnect when the attribute is destroyed?

Does an attribute connection (GetAttributeChangedSignal to be specific) disconnect when the attribute is destroyed?

No, unfortunately it does not.

After destroying, you can just do

Connection:Disconnect()

Example 1:

local TheGoodAttributeChanged = Instance:GetAttributeChangedSignal("GoodBoi"):Connect(function()
   print("Its good lol")
end)

task.wait(10)

Instance:SetAttribute("GoodBoi", nil)
TheGoodAttributeChanged:Disconnect()

Example 2:

local TheGoodAttributeChanged
TheGoodAttributeChanged = Instance:GetAttributeChangedSignal("GoodBoi"):Connect(function()
   Instance:SetAttribute("GoodBoi", nil)
   TheGoodAttributeChanged:Disconnect()
end)

How do i find the attribute’s signal to disconnect it after the player leaves? (The attribute is inside of the player’s character)

game:GetService("Players").PlayerRemoving:Connect(function(Player)
   TheGoodAttributeChanged:Disconnect() -- Just make sure as seen in my example it is a variable outside of a function.
end)

How would i store the signal if it can be overridden by other signals? (Since it would be outside functions like you said)

local Event

local function Whatever()
   Event = Part.Touched:Connect(function()
      print("Hi")
   end)
end

Event:Disconnect()

Wouldn’t the “Event” register globally? Which means other functions can interfere with this process if the script is in serverscriptservice

No, it would only register in that one script.

I don’t understand? If Player1 Joined they would assign “Event” To a rbxsignal and if player 2 joined they would override “Event” With their rbxsignal, Isn’t this how “local” works if it was outside?

Ah, I get it now.

If I get it correctly, you are looking for something like this:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	local Event: RBXScriptConnection
	-- Do whatever here
	Players.PlayerRemoving:Connect(function(LeavingPlayer)
		if Player == LeavingPlayer then
			Event:Disconnect()
		end
	end)
end)

That would cause to connect a “PlayerRemoving” Function to every new player that joins and that would count for everybody

Oops:

Probably need to look over a ton of my scripts then…

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	local Event: RBXScriptConnection
	-- Do whatever here
	local Removing: RBXScriptConnection
	Removing = Players.PlayerRemoving:Connect(function(LeavingPlayer)
		if Player == LeavingPlayer then
			Event:Disconnect()
			Removing:Disconnect()
		end
	end)
end)

Now this means you’re making a new connection to PlayerRemoving each time a player joins.

@OP if the attribute is in the player’s character though, wouldn’t the attributes and the connections go with the character? Just in case though, player.CharacterRemoving:Connect(game.Destroy) to be 100% sure the character gets destroyed, since :Destroy implicitly disconnects all connections.

1 Like

Does that cause performance issues even if you do :Disconnect() it? As far as I am aware, it shouldn’t.

1 Like

Ah, I thought it didn’t… Thanks for letting me know.

It should destroy the attribute’s connections since the attribute was destroyed with the character, but he stated the opposite

@sjr04 is probably a lot better than me. He has helped me with a ton of scripts in the past. I would go with his word rather than mine. Plus, it even says on the documentation for :Destroy(). I just knew Destroy from when I learned and never looked at this documentation.

Yes I am aware of that; I just thought the attributes might be a little different at some point

Nope! It will disconnect even if you destroy the Instance with the Attribute. It will also :Disconnect() all Descendants.