ScreenGui.Enabled not changed

When I write below the code then the code works correctly.
Example:
ToggleUI(“Off”) or ToggleUI(“On”)

But when I write this code, the console says, “ScreenGui.Enabled changed” but nothing has changed on my screen.
Example:
Button.MouseButton1Click:Connect(function()
print(“Working”)
ToggleUI(“Off”) or ToggleUI(“On”)
end)

local PS = game:GetService("Players")
local player = PS.LocalPlayer
local PGUI = player:WaitForChild("PlayerGui")
local Children = PGUI:GetChildren()

local UIMemory = {}

function ToggleUI(Type)
if Type == "On" then
for i = 1,#Children do
for x = 1,#UIMemory do
if UIMemory[x].Name == Children[i].Name then
Children[i].Enabled = true
print(Children[i].Enabled)
end
end
end
elseif Type == "Off" then
for i = 1,#Children do
if Children[i].ClassName == "ScreenGui" and Children[i].Enabled == true and Children[i].Name ~= "MainMenuGui" and Children[i].Name ~= "LoadingMenu" then
print(Children[i].Name)
Children[i].Enabled = false
print(Children[i].Enabled)
table.insert(UIMemory,Children[i])
end
end
end
end
1 Like

Maybe because you are setting Enabled to true? (In the “off” part)

1 Like

I think your problem is in the way you get the children of your GUI.

Try to change this:

local Children = PGUI:GetChildren()

To this:

local Children = {}

for i, child in pairs(PGUI:GetChildren()) do
	table.insert(Children, child)
end

I’m not 100% sure about that, but I think it could work.
Let me know if it works :slight_smile:

Your code doesn’t do anything. The two are exactly the same.

1 Like

Doing ToggleUI(“Off”) or ToggleUI(“On”) shouldn’t do anything since you are turning off your SGUI’s and then immediately turning them back on.

2 Likes

First of all, please consider formatting your code correctly w tabs. It makes your code so much easier to read !

I think the problem stems from the line

ToggleUI("Off") or ToggleUI("On")

In lua, what this does is it first calls the function ToggleUI(“Off”), then if this returns true, does nothing else. In this case, ToggleUI(“Off”) returns nil, so it will do ToggleUI(“On”) and it will also return nil, doing nothing after that. Basically, you’re turning the UI off then turning on again immediately after.

To fix this, add a debounce.

local on = false
Button.MouseButton1Click:Connect(function()
    print(“Working”)
    on = not on
    if on then
        ToggleUI("On")
    else 
        ToggleUI("Off")
    end
end)
1 Like

I was looking for a problem and I found it, it’s not a problem with the code, but the problem is that on one ScreenGui ResetOnSpavn is Enabled , and on the other not if the script is inserted in a ScreenGui where ResetOnSpawn is not Enabled then the script works only once.