Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you disconnect a function?

So I'm having troubles with some function

local function testF(Data)
	Mouse.Button1Down:Connect(function()
		print(Data)
	end)
end

testF("TestString")

--LATER ON

testF("NewTestString")

before this function is fired, clicking the mouse would NOT cause the output to print TestString

but once it is fired, clicking would then print TestString, how would I go about this so that this function is cancelled so that it would cease to print TestString, the function still believes the data is TestString, instead of NewTestStringdespite firing the function once again.

3 comments

0

pretty sure you mean disconnect an event :p

0

Yes, maybe, but I have no idea how I could do this.

0

:Disconnect()

1 answer

✓ Accepted answer

When you use :Connect it passes back a RBXScriptConnection that you can use to disconnect this function from the event.

Example

local con
local function testF(Data)
    con = Mouse.Button1Down:Connect(function() -- pass the RBXScriptConnection to the variable
        print(Data)
    end)
end

testF("TestString")

--LATER ON
con:Disconnect() -- disconnect this function from the event
testF("NewTestString")

Hope this helps.

2 comments

0

Although this works to disconnect the event, it appears that button1down doesn't work the second time :(

0

It should be working as it uses the same function to set it up?

This discussion is preserved as it was. New answers, comments, and votes are closed.