Disable Visible GUIs

Hello, I have a (invisible) ScreenGui that detects when a player does a combination of two keybinds, and I would like for it to be able to detect which ScreenGUIs are currently enabled in the Player’s PlayerGUI, and subsequently disable them. That part I could figure out, the issue is that I cannot figure out how the script could reenable only the GUIs that were disabled, as alongside visible GUIs, there would be invisible ones for keybinds in my game.

do you want it to be toggled or just always disabled, like if it was enabled should they be disabled and if it was disabled should it be enabled?

Well, you can do that by iterating through your Screen Guis using a for loop and catch the (visible/enabled) and put them into a temporary array and then you can disable/hide them after.
Is that what you were looking for?

Here is a simple example:

local PreviouslyVisible = {}
local PreviouslyInvisible = {}

local function ToggleVisibility(Player: Player)
	for _, ScreenGui in ipairs(Player.PlayerGui:GetChildren()) do
		if ScreenGui:IsA("ScreenGui") then
			if ScreenGui.Enabled then
				ScreenGui.Enabled = false
				table.insert(PreviouslyVisible, ScreenGui)
			else
				ScreenGui.Enabled = true
				table.insert(PreviouslyInvisible, ScreenGui)
			end
		end
	end
end

Yes, arrays seems like what I was looking for. From your example, any suggestions on how the local script could detect if there were items in the array?

Let’s say that at first, there are two enabled GUIs and three disabled GUIs. When someone first uses the keybinds to hide the enabled GUIs, there wouldn’t be items in the array until after the function, and the script should detect there is nothing inside, and then hide the enabled GUIs and add them into the array.

After another keybind input following the first one the script can re enable the GUIs that are listed as items in the array.

I’m not sure how to go about doing this as this is my first in doing anything with tables, apologies if my explaining is bad.

did you mean two enabled GUI’s and three disabled GUI’s?

ah yes, my mistake. yes, just for example

if you want to see if an array is empty:

i havent tested it tho

Sorry, but will you need to access the arrays? are they necessary?
I mean, you can just swap visibility without the need for making arrays.
my bad for the example.

well, I want to be able to enable only the ScreenGUIs that were previously enabled (stored in the array?), and not the ones meant to stay invisible

Only hide and remember the Guis which were visible, not invisible.

1 Like

Yeah, I think I messed up…

30000

Would this work in your case? I actually didn’t try it…

local PreviouslyVisible = {}

local function SwapVisibility(Player: Player)
	if #PreviouslyVisible >= 1 then
		local WereVisible = {}
		for _, ScreenGui in ipairs(Player.PlayerGui:GetChildren()) do
			if ScreenGui:IsA("ScreenGui") then
				if ScreenGui.Enabled then
					ScreenGui.Enabled = false
					table.insert(WereVisible, ScreenGui)
				end
			end
		end
		for i, ScreenGui in ipairs(PreviouslyVisible) do
			ScreenGui.Enabled = true
			table.remove(PreviouslyVisible, i)
		end
		PreviouslyVisible = WereVisible
	else
		for _, ScreenGui in ipairs(Player.PlayerGui:GetChildren()) do
			if ScreenGui:IsA("ScreenGui") then
				if ScreenGui.Enabled then
					ScreenGui.Enabled = false
					table.insert(PreviouslyVisible, ScreenGui)
				else
					ScreenGui.Enabled = true
				end
			end
		end
	end
end

Unfortunately, it doesn’t seem to work. It does disable the current enabled ScreenGUIs, but the second and following functions after the first keypress randomly enables GUIs, even the ones supposed to be disabled throughout (not sure of the actual reasoning)


To better show you what I mean.

For reference,
image

local PreviouslyVisible = {}

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
    if (input.KeyCode == Enum.KeyCode.LeftShift and UIS:IsKeyDown(Enum.KeyCode.C)) or (input.KeyCode == Enum.KeyCode.C and UIS:IsKeyDown(Enum.KeyCode.LeftShift)) then
        if #PreviouslyVisible >= 1 then
            local WereVisible = {}
            for _, ScreenGui in ipairs(player.PlayerGui:GetChildren()) do
                if ScreenGui:IsA("ScreenGui") then
                    ScreenGui.Enabled = false
                    table.insert(WereVisible, ScreenGui)
                end
            end
            for i, ScreenGui in ipairs(PreviouslyVisible) do
                ScreenGui.Enabled = true
                table.remove(PreviouslyVisible, i)
            end
            PreviouslyVisible = WereVisible
        else
            for _, ScreenGui in ipairs(player.PlayerGui:GetChildren()) do
                if ScreenGui:IsA("ScreenGui") then
                    if ScreenGui.Enabled then
                        ScreenGui.Enabled = false
                        table.insert(PreviouslyVisible, ScreenGui)
                    end
                end
            end
        end
    end
end)

Yeah yeah, sorry, I was trying to solve the issue and still…

No issues man, I appreciate the effort

Maybe try this function. I’ve tested it in studio with some Guis:

local PreviouslyEnabled = {}

local function SwapVisibilityV2(Player: Player)
	if #PreviouslyEnabled >= 1 then
		for _, ScreenGui in ipairs(Player:GetChildren()) do     -- you can use descendants if the guis are nested.
			if ScreenGui:IsA("ScreenGui") then
				local WasEnabled = table.find(PreviouslyEnabled, ScreenGui)
				if WasEnabled then
					ScreenGui.Enabled = true
					table.remove(PreviouslyEnabled, WasEnabled)
				else
					ScreenGui.Enabled = false
					table.insert(PreviouslyEnabled, ScreenGui)
				end
			end
		end
	else	--First time running
		for _, ScreenGui: ScreenGui in ipairs(Player.PlayerGui:GetChildren()) do
			if ScreenGui:IsA("ScreenGui") then
				if ScreenGui.Enabled then
					ScreenGui.Enabled = false
					table.insert(PreviouslyEnabled, ScreenGui)
				else
					ScreenGui.Enabled = true
				end
			end
		end
	end
end

Hey, sorry for the delay, I was busy with work.

I tested your code, but it inserted the disabled GUIs into the table and counted it as WasEnabled. I think you also made a mistake on the last ScreenGui.Enabled? because it seems that it’s supposed to be false.

local PreviouslyEnabled = {}

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
    if (input.KeyCode == Enum.KeyCode.LeftShift and UIS:IsKeyDown(Enum.KeyCode.C)) or (input.KeyCode == Enum.KeyCode.C and UIS:IsKeyDown(Enum.KeyCode.LeftShift)) then
        if #PreviouslyEnabled == 1 or #PreviouslyEnabled >= 1 then
            for _, ScreenGui in ipairs(player.PlayerGui:GetChildren()) do     -- you can use descendants if the guis are nested.
                if ScreenGui:IsA("ScreenGui") then
                    local WasEnabled = table.find(PreviouslyEnabled, ScreenGui)
                    if WasEnabled then
                        ScreenGui.Enabled = true
                        table.remove(PreviouslyEnabled, WasEnabled)
                        print(#PreviouslyEnabled)
                    else
                        ScreenGui.Enabled = false
                    end
                end
            end
        else	--First time running
            for _, ScreenGui: ScreenGui in ipairs(player.PlayerGui:GetChildren()) do
                if ScreenGui:IsA("ScreenGui") then
                    if ScreenGui.Enabled then
                        ScreenGui.Enabled = false
                        table.insert(PreviouslyEnabled, ScreenGui)
                        print(#PreviouslyEnabled)
                    else
                        ScreenGui.Enabled = false
                        print(#PreviouslyEnabled)
                    end
                end
            end
        end
    end
end)

This is my iteration with edits from your code, but in the end, it produced the effects I desired and worked. Much thanks for your help :+1:t2:

1 Like

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