Kob3eY
Kob3eY

Reputation: 1

Making a frame visible is possible but making invisible is not

      script.Parent.MouseButton1Click:Connect(function()
if script.Parent.Parent.Frame.Visible == true then
    script.Parent.Parent.Frame.Visible = false
end
if script.Parent.Parent.Frame.Visible == false then
    script.Parent.Parent.Frame.Visible = true
end
end)

I am very new to coding (have started practically yesterday ) and ive decided to start with lua in roblox studio. The program here is working i can assure you (some things in code may not be in proper place since im having a bit trouble writing code on this site) but the code works. I have tried to make it seperatly for 2 buttons then I tried this and nothing seems to work. I am starting to think it is caused by the program itself and not the code since I can make other things invisible with that code. I've placed those 2 codes for visibility and invisibility in 1 thing since I've been told that events caused by the code happen at same time so some of them may not work.

Upvotes: 0

Views: 458

Answers (1)

Luke100000
Luke100000

Reputation: 1539

Assume Visible is true. The first condition will be true and Visible will be set to false. Then, in the next condition Visible is false and the condition is therefore true, again. And it sets Visible back to true.

Now, in order to fix it you want to execute the second condition only if the first failed. Take a look at elseif https://www.lua.org/pil/4.3.1.html.

script.Parent.MouseButton1Click:Connect(function()
    if script.Parent.Parent.Frame.Visible == true then
        script.Parent.Parent.Frame.Visible = false
    elseif script.Parent.Parent.Frame.Visible == false then
        script.Parent.Parent.Frame.Visible = true
    end
end)

This code is still quite bulky and can be improved:

script.Parent.MouseButton1Click:Connect(function()
    script.Parent.Parent.Frame.Visible = not script.Parent.Parent.Frame.Visible
end)

Now you assign true to Visible if Visible does not evaluate to true. It basically toggles the boolean.

Upvotes: 2

Related Questions