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

Why isn't this debounce working?

Asked by 10 years ago

Why won't it do the false statement? Please show how you managed to fix it.

local Button = script.Parent.Button
------------------------------------
    IsTouched = false--Debounce
    ------------------------------------
        Button.Touched:connect(function()
                if not IsTouched then
                    ------------true statament------------
                    IsTouched = true
                        Button.BrickColor = BrickColor.new("Bright green")
                        Button.Size = Vector3.new(Button.Size.X,.5,Button.Size.Z)
                    IsTouched = false--Turns it back to false
                else
                    ---------------false statement---------
                    Button.BrickColor = BrickColor.new("Bright red")
                    Button.Size = Vector3.new(Button.Size.X,1.5,Button.Size.Z)

                end
        end)

2 answers

Log in to vote
2
Answered by 10 years ago

I kind of edited your script, it may work now, but if not let me know (PM me on Roblox (TheeDeathCaster));

local Button = script.Parent.Button
------------------------------------
IsTouched = false--Debounce
time = 2--Time before Debounce
------------------------------------
Button.Touched:connect(function()
if not IsTouched then
IsTouched = true
Button.BrickColor = BrickColor.new("Bright green")
Button.Size = Vector3.new(Button.Size.X,.5,Button.Size.Z)
wait(time)
IsTouched = false--Turns it back to false
Button.BrickColor = BrickColor.new("Bright red")
Button.Size = Vector3.new(Button.Size.X,1.5,Button.Size)
end
end)

Hope this helped!

Ad
Log in to vote
0
Answered by 10 years ago

Well, it would never run the false statement because the true statement instantly changed IsTouched to true and then false, without allowing time for any other option to exist. Here's a slightly cleaner version of what Alpha did:

local Button = script.Parent.Button 
local IsTouched = false--Debounce
------------------------------------

Button.Touched:connect(function() 
    if not IsTouched then 
    ------------true statament------------ 
        IsTouched = true 
        Button.BrickColor = BrickColor.new("Bright green") 
        Button.Size = Vector3.new(Button.Size.X,.5,Button.Size.Z)

        wait(0.5) -- waits half a second so that players and other scripts have time to react
        IsTouched = false--Turns it back to false 

    else 
    ---------------false statement--------- 
        Button.BrickColor = BrickColor.new("Bright red") 
        Button.Size = Vector3.new(Button.Size.X,1.5,Button.Size.Z) 
    end 
end) 

Answer this question