GUI doesn't appear even when enabled by localscript

Hi there, I have this script that toggles a GUI from a click of a button. Problem is, is that the GUI doesn’t actually appear however it shows that it is enabled in the properties. I’ve seen this is as a common issue with StarterGUI, however I’ve tried their solutions like re-defining the variable to local players then playergui however I can’t seem to get them working neither.

I’ll leave the original script here.

local open = true
local gui = game.StarterGui.FPS.Frame
local button = script.Parent.button

button.MouseButton1Click:Connect(function()
	if open == true then
		gui.Visible = true
		open = false
	else
		gui.Visible = false
		open = true
	end
end)
1 Like

Can you provide your attempt at using PlayerGui? Using StarterGui won’t change the player’s current Gui.

local open = true
local gui = game.Players.LocalPlayer.PlayerGui:WaitForChild("FPS").Frame
local button = script.Parent.button

button.MouseButton1Click:Connect(function()
	if open == true then
		gui.Visible = true
		open = false
	else
		gui.Visible = false
		open = true
	end
end)

When referencing UI objects, you need to use relative hierarchy (script.Parent.Parent -- etc.) instead of accessing it through game. This is because UI objects replicate to each player in the game.

3 Likes

Maybe it’s because PlayerGui didn’t exist yet. Try changing this:

local gui = game.Players.LocalPlayer.PlayerGui:WaitForChild("FPS").Frame

to this:

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

I see now. The other guy seemed to work for me but I’ll use this later when I need to organise my scripts really badly.

StarterGui is just a placeholder for all GUI objects that will be used in your game. The UI that is shown on a players screen is called the PlayerGui, and is a seperate copy of StarterGui. Therefore, you’re script won’t work because you’re referencing the wrong thing.

Just switch it to player.PlayerGui or use script.Parent in order to access the players UI

PlayerGui, like LocalPlayer, implicitly exists to the client. WaitForChild on PlayerGui isn’t necessary.

1 Like

That seemed to do the trick. It’s a bit messy but it works.

local player = game.Players.LocalPlayer
local playergui = player:WaitForChild("PlayerGui")
local gui = playergui.FPS
local buttonframe = script.Parent
local button = buttonframe.button

button.MouseButton1Click:Connect(function()
	gui.Visible = not gui.Visible
end)

Is this cleaner?

1 Like