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

Why my button doesn't work when i click on it? No problems in output?!What do i do?

Asked by
DashDQ 17
4 years ago
Edited 4 years ago

So basically if i click the button it would show a GUI, but if i click again again on it, it should close the GUI.Ok it shows the GUI only once but if i click again it stays frozen.GUI won't do anything after that.

Here is the script:

local button = script.Parent
local frame = script.Parent.Parent.Frame

button.MouseButton1Click:Connect(function()
    if frame.Visible == true then
        frame.Visible = false
    end
    if frame.Visible == false then
        frame.Visible = true 
    end
end)
0
Is it a serverscript or localscript? Qariter 110 — 4y
0
It is an local script DashDQ 17 — 4y

1 answer

Log in to vote
3
Answered by 4 years ago
Edited 4 years ago

It is because when the luau interpreter is gonna read that script, it is gonna see that the frame.Visible is true, run the statement and turn it false. Then, it is gonna see that frame.Visible is false, and turn it to true. What you should do to avoid this problem is:

local button = script.Parent
local frame = script.Parent.Parent.Frame

button.MouseButton1Click:Connect(function()

    if frame.Visible == true then -- if this runs, it is gonna turn false and not go through the else, if it doesn't, it is not gonna turn false and go to the else, which is gonna turn it true.

        frame.Visible = false

    else

        frame.Visible = true

    end

end)

Hope this helped!

0
It did!Thanks! DashDQ 17 — 4y
Ad

Answer this question