Why won't this script work when I click an image button?

script.Parent.MouseButton1Click:Connect(function()
	game.StarterGui.MainMenu.Enabled = false
end)

Not sure why this script wouldn’t work. No errors in the output either which is making it difficult for me to find the issue. I’m not great at scripting so it’s probably an obvious issue. Here’s my explorer.

3 Likes

Try making variables for the gui itself,

example:

local StarterGui = game:GetService("StarterGui")
local Gui = StarterGui:WaitForChild("MainGui")

script.Parent.MouseButton1Click:Connect(function()
Gui.Enabled = false
end)

The Gui which a player sees is not StarterGui, it is PlayerGui which is located under the LocalPlayer [LocalPlayer is only accessible through local scripts]. When the player’s Player.Character spawns for the first time all of the contents of StarterGui are automatically copied into the player’s PlayerGui.

Here you would do something like

local Player = game:GetService("Players").LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")

local MainGui = PlayerGui:WaitForChild("MainGui")

script.Parent.MouseButton1Click:Connect(function()
   MainGui.Enabled = false
end)
2 Likes

Basically you’re not supposed to change the StarterGui, instead you change the PlayerGui which is the gui player sees on their screen and you’d do that by doing this:

script.Parent.MouseButton1Click:Connect(function()
       game.Players.LocalPlayer.PlayerGui.MainMenu.Enabled = false
end)
2 Likes