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 8 years ago

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

01local buttonPressed = false
02 
03script.Parent.Touched:connect(function()
04if not buttonPressed then
05    print("BPT")
06 
07    buttonPressed = true
08    print("BPI")
09 
10game.Lighting.ClassicSlingshot:clone().Parent = game.Players.LocalPlayer.Backpack
11print("Hi")
12 
13buttonPressed = false
14print("BPF")
15 
16end
17end)
0
I got it from the wiki bossay8 8 — 8y

1 answer

Log in to vote
0
Answered by 8 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;

01local variable= false
02local part= script.Parent
03 
04part.Touched:Connect(function(hit)
05        local hum= hit.Parent:FindFirstChild("Humanoid")
06    if hum ~= nil and variable == false then variable = true
07        hum:TakeDamage(20)
08    end
09        wait(5)
10    variable = false
11end)

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

01local buttonPressed = false
02 
03script.Parent.Touched:connect(function()
04    if buttonPressed == false then
05        buttonPressed = true
06    game.Lighting.ClassicSlingshot:clone().Parent= game.Players.LocalPlayer.Backpack
07    wait(5)
08        buttonPressed = false
09    end
10end)
Ad

Answer this question