How do I cancel an "Instance.Changed:Connect(function() end)"

How do I cancel an “Instance.Changed:Connect(function() end)” so that it doesn’t react to that instance changing anymore?
I have something that looks like this

function()
–some stuff here–
Instance.Changed:Connect(function()
–some stuff here–
end)
–some stuff here–
return x
end

I can’t figure out how to cancel the Changed function inside once the entire function has finished because the events inside are still firing even though the function has ended and returned all the values I need it to. Any help would be appreciated, thank you!

Maybe try using break? break can help in some situations like this.

you can use :Disconnect()

for example:

local Connection = Instance.Changed:Connect(function()

end)

Connection:Disconnect() -- Disconnects the function

@Nerkonl

No, breaks wont help in this situation, breaks can only be utilized in loops, for example:

for i = 1, 10 do
    if i == 5 then
        break
    end
end 

but if it was trying to stop using a function you would get an error

local Connection = Instance.Changed:Connect(function()
    break -- errors here
end)
1 Like

Oh, alright I didn’t know that my bad.

can only be*

image

Sorry, just a nitpick on wording but that one word could be an important difference. Nonetheless, you are right in that break outside of a loop will throw an error.

2 Likes