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

Help with disconnecting a event?

Asked by 10 years ago

I was trying to disconnect two events each in a different block of code(Line 24,Line 50) but I got a error saying:

Event:disconnect() has been deprecated. Use connection object returned by connect()

How would I disconnect the event if it has been deprecated?

local BlueBar = script.Parent.GAME.BluPdl
local RedBar = script.Parent.GAME.RedPdl
local BlueSeat = script.Parent.Parent.Parent.Parent.BlueSeat
local RedSeat = script.Parent.Parent.Parent.Parent.RedSeat
----------------------BlueSeat----------------------------
BlueSeat.ChildAdded:connect(function(Part)--Checks if a Player has been steated
    if Part.Name == "SeatWeld" and Part.Part1.Name =="HumanoidRootPart" then
    ---------------------Vairbles-------------------------
    local Character = Part.Part1.Parent
    local Player = game.Players:GetPlayerFromCharacter(Character)
    local PlayersMouse = Player:GetMouse()
    ------------------------------------------------------
    PlayersMouse.KeyDown:connect(function(Key)
            if Key == "w" then
                if BlueBar.Position.Y.Offset > 0 then -- Rise Up if pressed w
                    BlueBar.Position = UDim2.new(0, 0, 0, (BlueBar.Position.Y.Offset - 20))
                end
            elseif Key == "s" then
                if BlueBar.Position.Y.Offset < 400 then
                    BlueBar.Position = UDim2.new(0, 0, 0, (BlueBar.Position.Y.Offset + 20))
                end
                BlueSeat.ChildRemoved:connect(function(Part)
                    if Part.Name == "SeatWeld" and Part.Part1.Name =="HumanoidRootPart" then
                        PlayersMouse.KeyDown:disconnect()
                    end
                end)
            end
        end)
    end
end)
----------------------RedSeat------------------------------
RedSeat.ChildAdded:connect(function(Part)--Checks if a Player has been steated
    if Part.Name == "SeatWeld" and Part.Part1.Name =="HumanoidRootPart" then
    ---------------------Vairbles-------------------------
    local Character = Part.Part1.Parent
    local Player = game.Players:GetPlayerFromCharacter(Character)
    local PlayersMouse = Player:GetMouse()
    ------------------------------------------------------
    PlayersMouse.KeyDown:connect(function(Key)
            if Key == "w" then
                if RedBar.Position.Y.Offset > 0 then
                    RedBar.Position = UDim2.new(0, 560, 0, (RedBar.Position.Y.Offset - 20))
                end
            elseif Key == "s" then--Rise down if s is pressed
                if RedBar.Position.Y.Offset < 400 then
                    RedBar.Position = UDim2.new(0, 560, 0, (RedBar.Position.Y.Offset + 20))
                end
                RedSeat.ChildRemoved:connect(function(Part)
                    if Part.Name == "SeatWeld" and Part.Part1.Name =="HumanoidRootPart" then
                        PlayersMouse.KeyDown:disconnect()
                    end
                    end)
            end
        end)
    end
end)

1 answer

Log in to vote
1
Answered by 10 years ago
--in order to disconnect an event, you must set a variable to the connection object returned by the event, like so
firstConnection = PlayersMouse.KeyDown:connect(function(Key)

--then you simply use the disconnect method on your connection object.
firstConnection:disconnect()


--it's a little different if you want to use a local variable. This breaks.
local secondConnection = PlayersMouse.KeyDown:connect(function(Key)

--instead, predefine a local variable, like so
local goodConnection
--then set the variable to the connection object.
goodConnection = PlayersMouse.KeyDown:connect(function(Key)


--finally, you can just disconnect your local variable.
goodConnection:disconnect()
Ad

Answer this question