GUI calling a nil value

I’m trying to make a GUI that opens another GUI when a button is pressed, and closes that GUI when a button is pressed.
This is the script that opens the GUI:

local menuButton = game.StarterGui.GameMenuGUI.GameMenu.MenuButton
local menu = game.StarterGui.MenuGUI.Menu
local menuExit = menu.ExitMenu

local function openMenu ()
	menu.Visible = true
	menuExit.Visible = true
	menuExit.Selectable = true
	menuExit.Active = true
end

menuButton.PressedImage:openMenu()

This is the script that closes the GUI:

local menuCloser = game.StarterGui.MenuGUI.Menu.ExitMenu
local menu = game.StarterGui.MenuGUI.Menu

local function closeMenu()
	menu.Visible = false
	menuCloser.Visible = false
	menuCloser.Selectable = false
	menuCloser.Active = false
end

menuCloser.PressedImage:closeMenu()

These are the errors I am getting:
Players.62penguins.PlayerGui.GameMenuGUI.GameMenu.MenuButton.MenuOpener:12: attempt to call a nil value - Client - MenuOpener:12

Players.62penguins.PlayerGui.MenuGUI.Menu.ExitMenu.MenuCloser:11: attempt to call a nil value - Client - MenuCloser:11

I have tried using PlayerGui instead of StarterGUI, but it still wont work. If you reply with a possible solution, please explain why it would work so that this can be constructive.

1 Like

can i see the explorer so i can test in studio and try to find solution from it

game.StarterGui

Should be enough.

Hey! There seems to be a few issues I see in why this script wouldn’t be working.

  1. .PressedImage is a PROPERTY not a event meaning you cant connect it to a function, instead try using one of these instead!
  1. Hoping that your script is on the client side, you don’t want to open every single players UI when one person click a button. If this is a local script, try learning about parents and children. Instead of starting your directory from the game try starting it from your exact script!

How do I know that it is client side?

As @EnvisionDev said, .PressedImage wont work, so instead do

menuButton.MouseButton1Up:Connect(openMenu)

When the function is already made outside of this line you need to do :Connect(openMenu), if the function is not made outside of it it will be

menuButton.MouseButton1Up:Connect(function()
   --Your code here
end

Same with close menu

You could also make it less confusing if the variables were called the same in both scripts, im talking about the menuCloser / menuExit Variables, somehow it made me very confused so do that

The server side is everything that happens on the server, basically what happens here will reply for every player in their screen, client side is the opposite of server side, so whatever that happens here will only reply for the player that is doing the action, Server Scripts are server side and Local Scripts are client side

It’s still not working. I don’t know why. Should I make the actual GUI instead of the frame visible?

is this in a local script or server script, and where did you place this script in?

It doesnt show any errors? you could try placing the scripts inside the frame, though, i would make both scripts into just one, since you can make it pretty easily, uhhhh, ill try to make it work wait me a sec :wink:

So, this is how ill do it, i’ll explain everything

Sorry if this seems like a lot of text but trust me, it would help a lot if you read everything, also, remember to change the info here to be accord to your project, or else it wont work

(Also, this script is a local script, so it only happens to the player who activated the button AND it is inside the TextButton)

local TextButton = script.Parent --Gets the button that its going to be touched
local ImageLabel = TextButton.Parent.ImageLabel --Basically your "menu" variable


local debounce = false --Information so the scripts knows if it has been activated before

TextButton.Activated:Connect(function() --When the button is Activated then Connect the action to a new function
	if not debounce then --Checks if it has activated before, if not then do the next code
		debounce = true --Set the information to true, so the next time it is activated the image wont show
		ImageLabel.Visible = true --dont have to explain this
		print("Image showed") --neither
	else --this says "if debounce is true (if the button has been activated before) then do this"
		debounce = false --set the information back to false so the next time the button is activated is going to repeat
		ImageLabel.Visible = false --bruh
		print("Image Removed") -- :/
	end
end) --this last ")" its because it was ( function(here it goes a parameter) and the close ")" goes here at the end
1 Like

It’s still not working. It won’t show any errors, but the print messages aren’t showing up. The only thing I can think of is that I have a script constantly running in the background, but I don’t know why that would mess it up because it has an hour long wait time in between each time it runs.

is the script that is running in the background in the same as where the TextButton script is?

cause if it is and as you say it has an hour wait then it wont run, you could try doing a coroutine and paste there your code that is running in the background

You should read this for more information for coroutines since im not an expert but basically it lets you run a code in the background while still running the code, imagine it like a road that has two sides, if one side is blocked, you can still go by the other side of the street

1 Like

No. It is not in the same place. Should I still use coroutines?

You mean like not in the same script, right?

Yes. It is a completely different script in server script service.

k, then dont use coroutines, can i see the code that you have now? just to see if i there’s any errors

I did what you told me to and cut it down to one script. Here it is:

local menuButton = script.Parent
local menu = game.StarterGui.MenuGUI.Menu

local menuOpen = false

local function openOrCloseMenu ()
	if not menuOpen then
		menuOpen = true
		menu.Visible = true
		print("Image Shown")
	else
		menuOpen = false
		menu.Visible = false
		print("Image Closed")
	end
end

menuButton.Activated:Connect openOrCloseMenu()

Sorry, it is showing the print messages now, but it won’t turn the menu visible.

Is this for the whole server to see or just the player?