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

Why is my debounce not performing correctly?

Asked by 5 years ago

As you can see, i'm trying to subtract "1" from the players alive value. Yet it always ends up somewhere in the negatives, like -9, -243, etc.

            Lava.Touched:Connect(function(findPlayer)
                if findPlayer.Parent:FindFirstChild("Humanoid") and debounce == false then
                    print(debounce)
                    print'player'
                    findPlayer.Parent:FindFirstChild("Humanoid").Health = 0
                    CurrentAlive.Value = CurrentAlive.Value - 1
                    wait(0.5)
                    debounce = true
                else
                    print'debounce = true, not false'
                end
            end)

2 answers

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

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

???

--// Variables
local debounce = false

Lava.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        if debounce == false then
            debounce = true
            hit.Parent:FindFirstChild("Humanoid").Health = 0
            CurrentAlive.Value = CurrentAlive.Value - 1
            wait(.5)
            debounce = false
        else
            print("Its true")
        end
    end
end)
Ad
Log in to vote
0
Answered by
Unhumanly 152
5 years ago
Edited 5 years ago

You're not using debounce properly. When you use debounce, you check to see if it's false. If it is, on the very next line, set it to true, so it can't be fired multiple times in a span of a few seconds.

Lava.Touched:Connect(function(findPlayer)
    if findPlayer.Parent:FindFirstChild("Humanoid") and debounce == false then
    debounce = true
        print(debounce)
        print'player'
        findPlayer.Parent:FindFirstChild("Humanoid").Health = 0
        CurrentAlive.Value = CurrentAlive.Value - 1
        wait(0.5)
        debounce = false
    else
        print'debounce = true, not false'
    end
end)

You're supposed to set debounce back to false at the end so code can be ran again. It was only working once because it only runs if debounce is false, and at the end, you set it to true and didn't modify it any further, which prevented it from running more than once.

I believe the reason the CurrentAlive value is going into the negatives is because this event listener is firing regardless of the Humanoid's health. You only want to remove one value from CurrentAlive when someone actually dies, not when a body part touches the lava.

Lava.Touched:Connect(function(findPlayer)
    if findPlayer.Parent:FindFirstChild("Humanoid") and debounce == false then
    debounce = true
        print(debounce)
        print'player'
    local Humanoid = findPlayer.Parent:FindFirstChild('Humanoid')
        if Humanoid and Humanoid.Health ~= 0 then
        findPlayer.Parent:FindFirstChild("Humanoid").Health = 0
            CurrentAlive.Value = CurrentAlive.Value - 1
    end
        wait(0.5)
        debounce = false
    else
        print'debounce = true, not false'
    end
end)

This section of code has been modified to make debounce work properly, and to only subtract 1 from CurrentAlive if the Player is touching this lava for the first time.

Now, there's a problem. If I'm not mistaken, you're trying to use a debounce to prevent the CurrentAlive value from subtracting more than 1 for 1 death. But at the same time, you're preventing other players from dying.

Lava.Touched:Connect(function(Hit)
    local Humanoid = Hit.Parent:FindFirstChild('Humanoid')
    if Humanoid then
        if Humanoid.Health ~= 0 then
            Humanoid.Health = 0
            CurrentAlive.Value = CurrentAlive.Value - 1
        end
    end
end)

There's the revised code. If I understand what you're trying to do, this should do it.

Answer this question