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

Why do I have to click it multiple times?

Asked by 7 years ago

*I've been trying to create a **ScreenGUI ** for when the players connect into my game that tells them some basic info, but for it to close the button has to be pressed multiple times and i'm not sure why!*

I am a beginner and well..I don't know what to do.

--0-
local WelcomeGUI = true
local Button     = script.Parent
local GUI = script.Parent.Parent.Parent.Frame
--0-

function CloseIt() --Start of the function
    if WelcomeGUI == true then
      GUI:TweenPosition(UDim2.new(0, 400,0, 200),"Out","Bounce")
    wait(0.15)
    WelcomeGUI = false
    elseif WelcomeGUI == false then
      GUI:TweenPosition(UDim2.new(0, 400,0, -200),"Out","Bounce")
    wait(0.15)
    WelcomeGUI = true
    end
end

Button.MouseButton1Down:connect(CloseIt)

2 answers

Log in to vote
0
Answered by 7 years ago

The problem is (I believe) related to your use of waits. Instead of using these waits, I would implement a debounce as it is more reliable:

--0-
local WelcomeGUI = true
local Button     = script.Parent
local GUI = script.Parent.Parent.Frame
local Debounce = true
--0-

function CloseIt() --Start of the function
    if not Debounce then return end -- This will exit the function if debounce is false.

    Debounce = false

    if WelcomeGUI == true then
        GUI:TweenPosition(UDim2.new(0, 400,0, 200),"Out","Bounce")
        WelcomeGUI = false
    else -- You can simply use an 'else' block here rather than an 'elseif'.
        GUI:TweenPosition(UDim2.new(0, 400,0, -200),"Out","Bounce")
        WelcomeGUI = true
    end

    wait(1) -- Tweens by default take 1 second.
    Debounce = true
end

Button.MouseButton1Down:connect(CloseIt)

Alternatively, you can use the override argument, like this:

    if WelcomeGUI == true then
        GUI:TweenPosition(UDim2.new(0, 400,0, 200),"Out","Bounce",1,true)
        WelcomeGUI = false
    else
        GUI:TweenPosition(UDim2.new(0, 400,0, -200),"Out","Bounce",1,true)
        WelcomeGUI = true
    end

This will mean that the player doesn't have to wait for the tween to finish before clicking again.

Ad
Log in to vote
0
Answered by 7 years ago

coool

Answer this question