Part-specific events use old reference after the reference is updated

I have a part, let’s call it Part A. I’ve created a variable in my script that references Part A, and a Touched event that’s connected to that variable. I have a duplicate of Part A in ReplicatedStorage. When Part A is destroyed, the duplicate in ReplicatedStorage is cloned into Workspace, and Part A’s reference is updated to the cloned part. However, the Touched event will still refer to the original Part A and therefore will not trigger anymore.

I also have some code that moves another part, Part B, to Part A’s position every frame. This code also uses the variable created earlier that refers to Part A. After the reference is updated, this code still works, i.e. Part B moves to the new Part A every frame as expected. This proves the reference was updated correctly.

How do I make part-related events update?

Connections doesnt rely on references, and it cant be reconnected to other event.
You can save function to variable in order to remove previous connection and create new connection

example:

local function onTouch(Part)
--something
end

local connection = partA.Touched:Connect(onTouch)

--something

connection:Disconnect()
connection = partC.Touched:Connect(onTouch)

if you destroying PartA threw method :Destroy(), :Disconnect() isn’t needed

1 Like

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