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

How can I disconnect an event/function that is connected using :connect(function)?

Asked by 4 years ago
script.Parent.Activated:Connect(function()
    local animation = script.Parent.Animation
    local animationtrack = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(animation)
    animationtrack:Play()
        script.Parent.Handle.Touched:Connect(function(Hit)
                if animationtrack.IsPlaying then
            Hit.Parent:FindFirstChild("Humanoid"):TakeDamage(5)
                else
                script.Parent.Handle.Touched:Disconnect() -- problem
        end 
    end)
end)

I am trying to disconnect the event, but it just gives me an error instead.

0
ahem... Fate2Seal 5 — 4y
0
Your answer doesn't even work. I am not accepting something that is broken. Thesquid13 301 — 4y
0
first of all add a local with the name you want, like local event = script.Parent.Activated:Connect(function() then after that do event:Disconnect() outside but not inside th ecode cherrythetree 130 — 4y

3 answers

Log in to vote
1
Answered by
mc3334 649 Moderation Voter
4 years ago
Edited 4 years ago

I feel bad for the amount of crap answer you have got. So this is A way to do it. Its what I use. We just create a bool variable and check it when the function is ran

disconnected = false

script.Parent.Touched:Connect(function(touch)
    if disconnected then return end --We check to see if the function has been "Disconnected"
    --Code here
end)

If you need to actually disconnect the event, do something like this:

-- "PartTouched" now represents what "connect()" returns, which is the event signal for this individual event.
local PartTouched = Part.Touched:connect(function()
    print("Part touched")
end)

-- Once you have a reference to that signal, you can then disconnect it by calling the "disconnect()" method on it whenever you want.
PartTouched:disconnect()

You can set the disconnected to true or false at your descretion/as needed. I hope I was able to help. If you have any questions, feel free to comment to my post.

0
How is this answer better than mine? He is obviously trying to disconnect handle.Touched connection, after main part (scrip.Parent) has been touched. Those are 2 different connections. You have just provided him with some stock code, while I gave him a working solution. sleazel 1287 — 4y
0
Because you literally said: "like this". Give some context to your answers. Its great to now how to do it, but you dont benefit if you dont understand what your actually doing mc3334 649 — 4y
Ad
Log in to vote
-1
Answered by
Despayr 505 Moderation Voter
4 years ago
Edited 4 years ago

since you have connected the function on the same line, it will not work

local Initiate = function()
    print("hi")

end

local connection = script.Parent.Touched:Connect(Initiate)

wait(1)

connection:Disconnect()

So you would do something like

local function TouchEvent(hit)

    if animationtrack.IsPlaying then
        hit.Parent:FindFirstChildOfClass("Humanoid"):TakeDamage(5)
    else
        connection:Disconnect()
    end
end

local connection = script.Parent.Handle.Touched:Connect(TouchEvent) --connects the event to the function
Log in to vote
-1
Answered by
sleazel 1287 Moderation Voter
4 years ago
Edited 4 years ago

Like this:

script.Parent.Activated:Connect(function()
    local animation = script.Parent.Animation
    local animationtrack = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(animation)
    animationtrack:Play()
    local connection = script.Parent.Handle.Touched:Connect(function(Hit)
        if animationtrack.IsPlaying then
            local humanoid = Hit.Parent:FindFirstChild("Humanoid")
            if humanoid then -- added extra check here, otherwise your script may error when touched by a non humanoid
                humanoid:TakeDamage(5)
            end
        else
            connection:Disconnect()
        end 
    end)
end)

Edit:Consider adding debounce.

Answer this question