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

Why isn't my stop (Debounce) working?

Asked by 8 years ago

Hi I was making a shop script and when I started to put a debounce in it all it did was when I opened the shop and tried to close it. The close button didn't work. Can you help?

local frame = script.Parent.Parent.Parent.Frame2
local stop = false
function click()
    if not stop then 
        stop = true
            if frame.Visible == false then
        script.Parent.Text = "Close"
        frame.Visible = true

            frame:TweenPosition(UDim2.new(0, 300, 0, 0), "Out", "Back", 1)
    elseif frame.Visible == true then
        script.Parent.Text = "Shop"
    frame:TweenPosition(UDim2.new(0, 300, 0, -350), "Out", "Quad", 1)
    stop = false
    wait(0.9)
    frame.Visible = false

            end
            end
end
script.Parent.MouseButton1Click:connect(click)
0
Btw, you should consider reducing the tween time. Lacryma 548 — 8y

1 answer

Log in to vote
1
Answered by
Lacryma 548 Moderation Voter
8 years ago

The problem is the stop variable.

The condition you set, if not stop then

makes it so that the none of the code that hides the frame runs.

A simple solution is to rearrange how you organized your script. Instead of limiting the elseif condition to run when stop is false, we will allow it to run if the stop variable is true.

Also a tip, you do not have to check for "== true" instead, you use if (condition) then or if (not condition) then.

local frame = script.Parent.Parent.Frame
local stop = false
function click()
   if not stop then
       stop = true
       if not frame.Visible then
            script.Parent.Text = "Close"
            frame:TweenPosition(UDim2.new(0, 300, 0, 0), "Out", "Back", 1)
            wait(1)
            frame.Visible = true
            stop = false
        else
            script.Parent.Text = "Shop"
            frame:TweenPosition(UDim2.new(0, 300, 0, -350), "Out", "Quad", 1)
            wait(1)
            frame.Visible = false
            stop = false
        end
   end
end
script.Parent.MouseButton1Click:connect(click)
Ad

Answer this question