Still have questions? Join our Discord server and get real time help.
0

How do you disconnect a function?

Asked by 2 months ago

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.

0
pretty sure you mean disconnect an event :p theking48989987 1669 — 2mo
0
Yes, maybe, but I have no idea how I could do this. Marmalados 164 — 2mo
0
:Disconnect() greatneil80 967 — 2mo

4
kingdom5 5274
2 months ago

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.

0
Although this works to disconnect the event, it appears that button1down doesn't work the second time :( Marmalados 164 — 2mo
0
It should be working as it uses the same function to set it up? kingdom5 5274 — 2mo