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

Disconnecting an event does not work?

Asked by 5 years ago
Edited 5 years ago

Hello there, I have a question about what I am doing wrong. So AmouseClick() is a function that compares Mouse.Target and the humanoid's torso's distance between eachother. It says that if the distance is less than the range it would cause damage to Mouse.Target.Parent.Humanoid

My problem however is that I add debounce in a way different from yours, Instead I disconnect the event nested in the function as highlighted with a comment.

No errors, does not disconnect at all, (tried removing the reconnecting part and still doesn't disconnect) and just let's the player spam their mouse.

The debounce is running once. I'm not fully sure of how to debounce since I'm not sure if events still do listen while a wait() is yielding the script. Just think of it has people walking into a door closed, won't they still walk in once the door is open?

Note: variable mouseEvent is stated before this function and is assigned an event after this function.

local function AmouseClick()
    if Settings.Class == nil then
        return nil
    end
    print("Mouse Clicked")
    if not debounce then 
        if player.Team == game.Teams.Zombies then
            if mouse.Target.Parent:FindFirstChild("Player") then
                local targetPlayer = game.Players:GetPlayerFromCharacter(mouse.Target.Parent)
                if targetPlayer.Team == game.Teams.Survivors then
                    local distance = (character.HumanoidRootPart.Position - mouse.Target.Position).Magnitude
                    if distance < Settings.Range then
                        local humanoid = mouse.Target.Parent.Humanoid
                        humanoid:TakeDamage(Settings.Damage)
                    end
                end

            end
        end
        debounce = true
    else
        print("Disconnecting mouse event")      --This is the problem
        mouseEvent:Disconnect()
        wait(Settings.AttackTime)                   
        mouseEvent = mouse.Button1Down:Connect(AmouseClick)
    end
end

mouse.Button1Down:Connect(AmouseClick)

Thank you for viewing this!

0
Is there no output whatsoever or is it printing "Disconnecting mouse event". Another thing, the debounce should be set true before doing all the checks to make sure that someone can't sneak in more than one click. Last thing, is debounce ever set back to false? sheepposu 561 — 5y

3 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

Your mouseEvent isn't assigned anything when the script runs. On line 29 you're simply stating that when the mouse is clicked it'll trigger. You need to actually assign mouseEvent on line 29 the same way you assigned it on 25 like:

mouseEvent = mouse.Button1Down:Connect(AmouseClick)

Also, the script will run regardless of if script is held up by a wait. I'm not sure on the specifics but let's put it like this, if you tell a script to print "Hello" after waiting 3 seconds and you rapidly clicked about 10 times. Then 3 seconds later you'd see "Hello" print 10 times.

Assuming I understand exactly what your script is intending to do, this should work:

local function AmouseClick()
    if Settings.Class == nil then
        return nil
    end

    print("Mouse Clicked")
    if player.Team == game.Teams.Zombies then
        if mouse.Target.Parent:FindFirstChild("Player") then
            local targetPlayer = game.Players:GetPlayerFromCharacter(mouse.Target.Parent)
            if targetPlayer.Team == game.Teams.Survivors then
                local distance = (character.HumanoidRootPart.Position - mouse.Target.Position).Magnitude
                if distance < Settings.Range then
                    local humanoid = mouse.Target.Parent.Humanoid
                                humanoid:TakeDamage(Settings.Damage)
                end
            end
        end
    end
    print("Disconnecting mouse event")
    mouseEvent:Disconnect()
    wait(Settings.AttackTime)
    mouseEvent = mouse.Button1Down:Connect(AmouseClick)
end

mouseEvent = mouse.Button1Down:Connect(AmouseClick)
0
A few things that could be tidied up about this code but the main thing to note was that I moved the mouse disconnect debounce to follow the actual script's effect because before it would've taken at least two mouse clicks to disconnect the mouse. This way, the script's effect runs, the mouse is disconnected and then waits all on the first click XxTrueDemonxX 362 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

You have to define Mouse Event before you do anything with it. That's like reading a book backwards.

 print("Disconnecting mouse event")      --This is the problem
    mouseEvent = mouse.Button1Down:Connect(AmouseClick)
    wait(Settings.AttackTime)  
    mouseEvent:Disconnect()                 
0
Unfortunately that did not work, I ended with the same result but it lagged my game (aka player is able to spam click). I'm not sure why it is not disconnecting in the first place though. 123nabilben123 499 — 5y
0
Change if mouse.Target.Parent:FindFirstChild("Player") then to if mouse.Target.Parent:FindFirstChild("Humanoid) then JayShepherdMD 147 — 5y
0
This whole script is very confusing. JayShepherdMD 147 — 5y
0
mouse.Target.Parent:FindFirstChild("Player") is an object that tells if it is a player or not. The reason I did this is to not make npcs take damage and only players. Sorry if that messed you up there 123nabilben123 499 — 5y
Log in to vote
0
Answered by 5 years ago

See if this works

local function AmouseClick()
    if Settings.Class == nil then
        return nil
    end
    print("Mouse Clicked")
    if not debounce then 
        if player.Team == game.Teams.Zombies then
            if mouse.Target.Parent:FindFirstChild("Player") then
                local targetPlayer = game.Players:GetPlayerFromCharacter(mouse.Target.Parent)
                if targetPlayer.Team == game.Teams.Survivors then
                    local distance = (character.HumanoidRootPart.Position - mouse.Target.Position).Magnitude
                    if distance < Settings.Range then
                        local humanoid = mouse.Target.Parent.Humanoid
                        humanoid:TakeDamage(Settings.Damage)
                    end
                end

            end
        end
        debounce = true
    else
        print("Disconnecting mouse event")      --This is the problem
        mouseEvent:Disconnect()
        wait(Settings.AttackTime)                   
        mouseEvent = mouse.Button1Down:Connect(AmouseClick)

        debounce = false --//We have to make sure that the debounce gets reset or the function won't work the way it was intended.
    end
end

mouseEvent = mouse.Button1Down:Connect(AmouseClick)

Answer this question