Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How can I disconnect a function?

Asked by 10 years ago

How can I disconnect a function? For example;

function oc(ack)
find=ack.Parent:FindFirstChild("Humanoid")
if find then
find.Health=50
end end

script.Parent.Touched:connect(oc)
script.Parent.TouchEnded:disconnect(oc) --Would this work? If not, how can I disconnect the function?

3 answers

Log in to vote
3
Answered by
Destrings 406 Moderation Voter
10 years ago

Connections are actually instances of RBXScriptSignal. Creating one (with connect) returns a instance of RBXScriptConnection. And there's a method that allows you to disconnect them.

So

connection = script.Parent.Touched:connect(function(part)
    print(part.Name)
    connection:disconnect()
end)
0
Wow, I never really knew that until now. Thanks man! +1 TheeDeathCaster 2368 — 10y
Ad
Log in to vote
0
Answered by 10 years ago
game.Workspace.Yellow.Touched:connect(function(ack)
find=ack.Parent:FindFirstChild("Humanoid")
if(find)then
    find.Health=50
end
end)

Not too sure what you meant by disconnect but I guessed you meant make it only run once and if so no code will run twice without a loop inside of it. The code above is a tad more efficient that your own and will only run once due to the non-existence of a loop.

0
You are wrong. That code can run more than once because it is being called every time the event fires, i.e when you touch the brick. Destrings 406 — 10y
Log in to vote
0
Answered by
hiccup111 231 Moderation Voter
10 years ago

There's only two methods I can think of doing this, (although you'd might want to look up breakpoints)

Method 1

Call a function that doesn't exist, it's messy, but it does the job

part = Workspace.Part

part.Touched:connect(function(hit)

    --code code code...

    callaFuntionThatDoesnExist()
end)

Method 2

Use a debounce

part = Workspace.Part

active = true

part.Touched:connect(function(hit) if not active return end

    --code code code...

    active = false
end)

Answer this question