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