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.

01Lava.Touched:Connect(function(findPlayer)
02    if findPlayer.Parent:FindFirstChild("Humanoid") and debounce == false then
03        print(debounce)
04        print'player'
05        findPlayer.Parent:FindFirstChild("Humanoid").Health = 0
06        CurrentAlive.Value = CurrentAlive.Value - 1
07        wait(0.5)
08        debounce = true
09    else
10        print'debounce = true, not false'
11    end
12end)

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.

???

01--// Variables
02local debounce = false
03 
04Lava.Touched:Connect(function(hit)
05    if hit.Parent:FindFirstChild("Humanoid") then
06        if debounce == false then
07            debounce = true
08            hit.Parent:FindFirstChild("Humanoid").Health = 0
09            CurrentAlive.Value = CurrentAlive.Value - 1
10            wait(.5)
11            debounce = false
12        else
13            print("Its true")
14        end
15    end
16end)
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.

01Lava.Touched:Connect(function(findPlayer)
02    if findPlayer.Parent:FindFirstChild("Humanoid") and debounce == false then
03    debounce = true
04        print(debounce)
05        print'player'
06        findPlayer.Parent:FindFirstChild("Humanoid").Health = 0
07        CurrentAlive.Value = CurrentAlive.Value - 1
08        wait(0.5)
09        debounce = false
10    else
11        print'debounce = true, not false'
12    end
13end)

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.

01Lava.Touched:Connect(function(findPlayer)
02    if findPlayer.Parent:FindFirstChild("Humanoid") and debounce == false then
03    debounce = true
04        print(debounce)
05        print'player'
06    local Humanoid = findPlayer.Parent:FindFirstChild('Humanoid')
07        if Humanoid and Humanoid.Health ~= 0 then
08        findPlayer.Parent:FindFirstChild("Humanoid").Health = 0
09            CurrentAlive.Value = CurrentAlive.Value - 1
10    end
11        wait(0.5)
12        debounce = false
13    else
14        print'debounce = true, not false'
15    end
16end)

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.

1Lava.Touched:Connect(function(Hit)
2    local Humanoid = Hit.Parent:FindFirstChild('Humanoid')
3    if Humanoid then
4        if Humanoid.Health ~= 0 then
5            Humanoid.Health = 0
6            CurrentAlive.Value = CurrentAlive.Value - 1
7        end
8    end
9end)

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

Answer this question