Simple show/hide gui script not working

Hey guys! I’m working on a simple script that I should be able to write but for whatever reason can’t. The purpose is to, if a GUI is visible, to hide the GUI, and if the GUI is hidden, then to show the GUI. So basically it changes the visibility to the opposite.

My script is here, it doesn’t error.

local vis = script.Parent.Parent.ShopFrame.Visible

script.Parent.MouseButton1Down:Connect(function()
	if vis==false then
		script.Parent.Parent.ShopFrame.Visible=true
	
	else if vis==true then
		script.Parent.Parent.ShopFrame.Visible=false
		end
		end
end)

It’s really simple, but what am I doing wrong. (This is a LocalScript in button GUI btw)

2 Likes

I’m really confused what your problem is

2 Likes

My script isn’t showing/hiding the GUI, it isn’t working.

1 Like

maybe try removing the Else if statement, it’s really not necessary, it’s a true or false value so just use ‘Else then’

1 Like

Use MouseButton1Click instead of MouseButton1Down

1 Like

That’s not the problem, what you’re doing is adding an infinite yield because you’re saying whenever the GUI= visible then make it false but if it isn’t visible make it visible and it’s just repeating that infinitely

1 Like

I just added a cross out button, that way it looks neater in my case and it makes it much less complicated.

1 Like

Yeah, that’s probably a better idea

1 Like

Just do:

local shopFrame = script.Parent.Parent.ShopFrame

script.Parent.MouseButton1Down:Connect(function()
shopFrame.Visible = not shopFrame.Visible
end)

Make sure it’s a local script.

1 Like

That isn’t the problem, the problem is that they’re assigning a static value (whatever Boolean is assigned to the GuiObject’s ‘Visible’ property) to a variable named “vis”, this variable is not a reference to the GuiObject’s ‘Visible’ property but instead a reference to its initial ‘Visible’ property’s value. The solution is fairly trivial, they just need to make the variable point/refer to the GuiObject itself, that way it can be used to fetch the current state of the GuiObject’s ‘Visible’ property whenever necessary.

local Script = script
local GuiObject = Script.Parent

print(GuiObject.Visible) --false
GuiObject.Visible = true
print(GuiObject.Visible) --true
1 Like

Maybe try this?? It’s untested though.

local frame = script.Parent.Parent.ShopFrame

script.Parent.MouseButton1Down:Connect(function()
	if frame.Visible == false then
		
frame.Visible = true
	
	else if frame.Visible == true then
		frame.Visible = false
		end
end)

Is the button activating in the first place? If it is not, try and do button.Activated:Connect(function) instead.

Here is the much MUCH more simpler script:

script.Parent.MouseButton1Down:Connect(function()
	script.Parent.Parent.ShopFrame.Visible = not script.Parent.Parent.ShopFrame.Visible
end)