Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How do you disconnect a function?

Asked by 5 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 1808 — 5mo
0
Yes, maybe, but I have no idea how I could do this. Marmalados 163 — 5mo
0
:Disconnect() greatneil80 1140 — 5mo

1 answer

Log in to vote
4
Answered by
kingdom5 5316 Trusted Snack Break Moderation Voter Community Moderator
5 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 163 — 5mo
0
It should be working as it uses the same function to set it up? kingdom5 5316 — 5mo
Ad

Answer this question