Buttons are not opening the frames when clicked in surface gui

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want to get the buttons to open the frame

  2. What is the issue? the 4 buttons in this screen shot are not opening when clicked
    image
    image

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub? i tried to debug my code

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local Frame = game.StarterGui.SurfaceGui
local CurrentFrame = nil

for _, button in pairs(Frame:GetChildren()) do
	print(button.Name)
	if button:IsA("TextButton") then
		button.MouseButton1Click:Connect(function()
			local relatedFrame = nil
			for _, frame in ipairs(Frame:GetChildren()) do
				if frame:IsA("Frame") then
					if frame.Name == button.Name then
						relatedFrame = frame
					end
				end
			end

			if relatedFrame then
				if relatedFrame == CurrentFrame then
					CurrentFrame.Visible = false
					CurrentFrame = nil
				elseif CurrentFrame then
					CurrentFrame.Visible = false
					CurrentFrame = relatedFrame
					CurrentFrame.Visible = true
				else
					CurrentFrame = relatedFrame
					CurrentFrame.Visible = true
				end
			end
		end)
	end
end

Forgive me if this is already taken into account, but you have a surface GUI in StarterGUI. I believe that it needs to be attached to a part to display. Also try changing MouseButton1Click to Activated. For debugging, can you attach print(relatedFrame) after your for loop?

This won’t refer to the SurfaceGui that the player sees and can interact with because GUIs in the StarterGui service are cloned and Parented / placed into the player’s PlayerGui folder.

This means that the reference to the Frame could be modified to be relative (e.g. if the LocalScript was placed directly into the SurfaceGui, it would be script.Parent) or it could be accessed through the Players Service → LocalPlayer → PlayerGui → SurfaceGui so that the script will be listening for inputs to the TextButtons within the SurfaceGui that the player has access to.