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

How do I add a debounce to this?

Asked by
FiredDusk 1466 Moderation Voter
8 years ago

When I touch a part, I get anchored. I want to have a debounce so it can wait about 1 second before it could get stepped on again.

script.Parent.Touched:connect(function(part) 

if part.Parent:FindFirstChild('Torso') then 
    part.Parent.Torso.Anchored = true 
    wait(15)
    part.Parent.Torso.Anchored = false

end 
end)

1 answer

Log in to vote
1
Answered by
dyler3 1510 Moderation Voter
8 years ago

So a debounce is basically a variable that you set to true or false depending on when you want something to happen. So in your situation, we need to allow the function to work once, and then after it's done wait 1 second, then allow it again.


To do this, we can use any variable, but we'll just call it 'Debounce' for simplicity.

local Debounce = false --Sets the debounce variable

script.Parent.Touched:connect(function(part) 
    if part.Parent:FindFirstChild('Torso') then 
        if not Debounce then --Checks if it's currently allowed to pass
            Debounce = true --Sets to true so it wont happen again
            part.Parent.Torso.Anchored = true 
            wait(15)
            part.Parent.Torso.Anchored = false
            wait(1)
            Debounce = false --Sets it back to false so it's allowed to happen again.
        end 
    end
end)

So now your script should work. If you have any further problems/questions, please leave a comment below, and I'll see what I can do. Hope I helped :P

-Dyler3

Ad

Answer this question