Script that disables CoreGui when Menu is opened

Hey everyone,

I’ve been trying to make a script that disables the Roblox backpack and playerlist when a menu is open. However, the roblox backpack and playerlist continue to stay disabled even when the menu is gone.

local player = game.Players.LocalPlayer
local playerGui = player.PlayerGui
local coreGui = game.CoreGui

local function disableCoreGui()
	local StarterGui = game:GetService("StarterGui")
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)
end


local function enableCoreGui()
	local StarterGui = game:GetService("StarterGui")
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, true)
end


local function checkParentGuiOpen()
	if playerGui:FindFirstChild("Menu") then
		disableCoreGui()
	else
		enableCoreGui()
	end
end


game:GetService("RunService").RenderStepped:Connect(checkParentGuiOpen)

How many starter gui’s are in the workspace?

In this specific scenario, does the menu get deleted from “playerGui” when its closed?
Because from what I am seeing, you are just checking if it exists, not if its opened. Try this:
if playerGui:FindFirstChild(“Menu”).Enabled then

*I would also like to mention like Haystees that running this function every frame will not be optimal. Add a connection to check when the GUIs states changes:
playerGui.Menu:GetPropertyChangedSignal(“Enabled”):Connect(checkParentGuiOpen)

1 Like

Ummm, you should DEFINITELY not use RenderStepped to check if its open, and rather use
GuiObject.Visible
https://create.roblox.com/docs/reference/engine/classes/GuiObject#Visible
(If its a ScreenGui use .Enabled instead of .Visible)

2 Likes

As well as use :GetPropertyChangedSignal() to detect when it changes, then change the CoreGui accordingly, like @PowWow_TheGreat did.

player:WaitForChild("PlayerGui").Menu.Changed:Connect(function(Val)
   if Val == "Enabled" then
      if player.PlayerGui.Menu.Enabled == true then
          	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)
      else
          	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, true)
      end
   end
end)

Unfortunately, This script did not work.

At least with my personal experience, using WaitForChild to get the PlayerGui of a player does not work for literally anything. I’m unsure if PlayerGui can actually be retrieved in a script using WaitForChild. That should be changed to player.PlayerGui

Secondly, this is personal preference and is up to you, but I would recommend :GetPropertyChangedSignal instead of .Changed

I also recommend breaking things (the PlayerGui and menu) down into variables. This is especially good for if you change anything in the future. You’d only need to change one line instead of a plethora.


@Michyays

Here is the full updated code
local Players = game:GetService("Players")
local starterGui = game:GetService("StarterGui")

local player = Players.LocalPlayer

local playerGui = player.PlayerGui
local menu = playerGui:WaitForChild("Menu")

menu:GetPropertyChangedSignal("Enabled"):Connect(function()
	if menu.Enabled then
		starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)
	else
		starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, true)
	end
end)

This should work, but if it does not, please let me know.


Also @weakroblox35, I apologize if I sounded rude or upset you for correcting/changing your code.

1 Like

Thank you so much, this should be placed in a LocalScript within StarterGui, correct?

It does not necessarily matter as long as it is somewhere that allows local scripts to actually run.

If that is all you need for the entire script, I would place it inside of StarterPlayerScripts to prevent clutter in the StarterGui

That is literally how you are supposed to get the player gui. I’ve never had any problems. Not using a WaitForChild call will break your scrip if you try to retrieve before the player loads.

I’ve never actually had WaitForChild with the PlayerGui work for me. I’m not kidding. Especially in the last week, I have tried using it, but it had no result, but when I removed WaitForChild it worked just fine. I’m unsure why. I assume it is an engine bug or something.

And here is all of the places LocalScripts can run if you are curious:
image

Here is the documentation link as well

Yes because you have fast internet, playergui is not created immediately. if a laggy person joins and playergui doesn’t load the whole script breaks. I can prove this because when I was testing my whole script broke due to the game took a little too long too load while making playergui not loading in yet.

I’m not saying this is the issue. The script breaks whenever I use WaitForChild to get the PlayerGui, which it shouldn’t do at all. It should still get the PlayerGui using WaitForChild, but it won’t. I’ve tested on my computer, phone, and phone using data instead of wifi (which is really slow. 5G sucks). After 10 minutes, it still did nothing, and no errors either. It would have timed out by then.

What I am saying is that it doesn’t call the PlayerGui at all, but removing WaitForChild works just fine. What I am trying to say is probably completely different from what you are getting from it.

ok then explain why when i use this
local gui = game:GetService("Players").LocalPlayer.PlayerGui
and join with laggy pc and/or internet, it throws attempt to index nil? PlayerGui does not load immediately. It is created on playeradded.

That is what happens when it doesn’t load, and yes, that happens for me using that on occasion. That is why you’d be smart and use WaitForChild, but let me try to explain what happens when I use WaitForChild. I may not be explaining well. English is not my first language, and I apologize.


And I am aware of this. You are telling information I already know.


Now let me try to explain better:

This works just fine on my computer obviously. Really fast internet, loads immediately and causes no isses. However, on my phone it throws the error attempt to index nil because it does not exist yet.

But when I use this

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

It does not work at all. It does not return an error and the rest of the script runs. What I’m saying with my personal issue, it just does nothing. None of the rest of the code that changes the UI runs, but if I change it back to LocalPlayer.PlayerGui, it works just fine.
I do not understand why as using WaitForChild should work with it normally, and it has for me since I’ve started scripting in roblox. It has only stopped in the last about week.

How? It just waits for the object to load, you can add a timeout too. WaitForChild("", 99) will assure IT LOADS.

Trust me. I’ve asked this same thing. Even punched my monitor about it (I get overly aggressive with stupid things sometimes). I don’t know why or how. And like you said, I’ve tried adding a timeout. Nothing. It just stopped working, and literally only with PlayerGui objects. Nothing else

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.