how do I disconnect a specific event

Viewed 53

I have a module script that looks something like this:

return {
  coolfunc = function(object)
    RunService.Stepped:Connect(function()
      print(object)
    end)
  end
}

and I call this script multiple times, how do I disconnect a specific connection?

1 Answers

The Connect method will return an connection object.

return {
  coolfunc = function(object)
    return RunService.Stepped:Connect(function()
      print(object)
    end)
  end
}

And you need store it somewhere.

connection = xxx.coolfunc(obj)

Then you can call the Disconnect method to disconnect the event.

if connection.Connected then
    connection:Disconnect()
end
Related