Help with PlayerGui

Hey there,

So upon editing the Core Gui’s for my game (removing the leaderboard, chat etc.). So when the player is in the menu all Core Gui’s are meant to be invisible. But upon testing my game I got this error:

Main is not a valid member of PlayerGui "Players.pinchpotmonster.PlayerGui"

Here is the script:

local StarterGui = game:GetService("StarterGui")
local player = game.Players.LocalPlayer

if player.PlayerGui.Main.Menu.Visible == true then
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, true)
else
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, true)
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, true)
end

The script is located in StarterPlayerScripts.

Thank you for the assistance. :slight_smile:

Try using WaitForChild?

local StarterGui = game:GetService("StarterGui")
local player = game.Players.LocalPlayer
local plrgui = player:WaitForChild("PlayerGui")
local main = plrgui:WaitForChild("Main")

if main.Menu.Visible == true then
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, true)
else
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, true)
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, true)
end

If you get that error on Menu, just WaitForChild on Menu as well

That seems to only put a stop to the errors occurring within the Output. Though the problem still continues, The leaderboard, chat, player inventory remain visible whilst the menu is still open.

Also I am sorry I made a small typo in the code that doesn’t effect it really but the error was:

StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, true)

When it was meant to be:

else
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, true)
end

Sorry if this came off as rude or disrespectful it was not meant to come off that way.

EDIT:
I re-tested and the output gave this error:
Infinite yield possible on 'Players.pinchpotmonster.PlayerGui:WaitForChild(“Main”)

Can you show us your PlayerGui layout? If it can’t find the UI inside of it, something might not be correct.

Yeah sure,

Screen Shot 2021-04-17 at 7.21.22 pm

Thats everything inside of PlayerGui

Is “Main” inserted into the PlayerGui via StarterGui? Or do you insert it later on

It is inserted through StarterGui

How odd. I’m gonna open up a baseplate and see if I can replicate the error

1 Like

The error is because the script fires so quickly the GUI doesn’t have time to be replicated properly just to prove that it works just add a wait(n) at the start of the script, obviously this is not the solution but it will give you an idea what do you have to do, the script gets replicated and starts working much faster than the GUI itself

image
On my first attempt (with the original code) I got the exact same error.

image
However I managed to get the script to work using WaitForChild() as @EmbratTheHybrid suggested.

I seems to be able to find “Main” without a problem

I’m beginning to think that there may be something interfering with the code
My Attempt (23.8 KB)

Would it be possible to have a full look at the scripts?
(of course if you’re not comfortable sharing your code that is understandable.)

Maybe this will work?

local StarterGui = game:GetService("StarterGui")
local player = game.Players.LocalPlayer
local PlayerGui = player:WaitForChild("PlayerGui")
local main = PlayerGui:WaitForChild("Main")
local menu = main:WaitForChild("Menu")
wait(1)

if menu.Visible == true then
    game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All,true)
else
    game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All,false)
    game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList,true)
end

I added a:

wait(5)

@FlabbyBoiii
This is the full script:

local StarterGui = game:GetService("StarterGui")
local player = game.Players.LocalPlayer
local plrgui = player:WaitForChild("PlayerGui")
local main = plrgui:WaitForChild("Main")

wait(5)
if main.Menu.Visible == true then
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, true)
else
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, true)
end
1 Like

See if this works

local StarterGui = game:GetService("StarterGui")
local Main = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("Main")
local Menu = Main:WaitForChild("Menu")

if Main and Menu.Visible then
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, true)
else
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, true)
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, true)
end

The Chat and Player Inventory still remain invisible after the player chooses a team.

Try putting the wait(5) above the rest of the code/as the first line? This is seriously odd

All of the CoreGui’s remain visible.

Menu.Changed:Connect(function()
    if Main and Menu.Visible then
	    StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, true)
    else
	    StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, true)
	    StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, true)
    end
end)

If we connect this to the Changed event, then the CoreGuis will update as the state of Menu is changed.

that shouldn’t do anything you have the wait before the variables
I usually wait a frame before or just move the script in PlayerGUI as it will be replicated once with the GUI

local RunService = game:GetService("RunService")
local player = game.Players.LocalPlayer
RunService.Heartbeat:Wait()
local StarterGui = game:GetService("StarterGui")
local plrgui = player:WaitForChild("PlayerGui")
local main = plrgui:WaitForChild("Main")

if main.Menu.Visible == true then
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, true)
else
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, true)
end

The CoreGui’s still remain visible.

image
as you can see it works. it has something to do with your setcoreguienabled
edit: you set them to true, you have to set them to false