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)
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.
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
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)