Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is my debounce not performing correctly?

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

???

--// 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)

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.