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

Why doesn't the debounce work at all?

Asked by 7 years ago

I put debounce in my code, but it wont work and it gives the tool multiple times.

local buttonPressed = false

script.Parent.Touched:connect(function() 
if not buttonPressed then
    print("BPT")

    buttonPressed = true
    print("BPI")

game.Lighting.ClassicSlingshot:clone().Parent = game.Players.LocalPlayer.Backpack
print("Hi")

buttonPressed = false
print("BPF")

end
end) 
0
I got it from the wiki bossay8 8 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

Your script doesn't correctly define the debounce as it's supposed to be. The correct way would be to create an if statement making sure that you clarify if the variable is false, then you make it true. Once that variable is true, until it's false again it should not be able to work.

Example;

local variable= false
local part= script.Parent

part.Touched:Connect(function(hit)
        local hum= hit.Parent:FindFirstChild("Humanoid")
    if hum ~= nil and variable == false then variable = true
        hum:TakeDamage(20)
    end
        wait(5)
    variable = false
end)

I'll help you out by correcting your script as well, you can thank me later.

local buttonPressed = false

script.Parent.Touched:connect(function() 
    if buttonPressed == false then
        buttonPressed = true
    game.Lighting.ClassicSlingshot:clone().Parent= game.Players.LocalPlayer.Backpack
    wait(5)
        buttonPressed = false
    end
end) 
Ad

Answer this question