Need help cleaning up my games UI script

Im new to all this ui and stuff and im just wondering if theres a cleaner way of doing this, I’ve got a page type system for the menu and my code is just so ridiculous i would have to think theres a better way.

local function mainmenu()
	mode = 'main'
	Bar.Editor.Visible = false
	Bar.Deck.Visible = false
	Bar.Wheels.Visible = false
	Bar.Trucks.Visible = false
	Bar.MainMenu.Visible = true
	Bar.Back.Visible = false
	MainMenu.EditorButton.Visible = true
	MainMenu.PlayButton.Visible = true
	MainEditor.deckButton.Visible = false
	MainEditor.truckButton.Visible = false
	MainEditor.wheelButton.Visible = false
	WheelEditor.wheelblack.Visible = false
	WheelEditor.wheelgray.Visible = false
	WheelEditor.wheelwhite.Visible = false
	TruckEditor.truckblack.Visible = false
	TruckEditor.truckgray.Visible = false
	TruckEditor.truckwhite.Visible = false
	TruckEditor.trucknormal.Visible = false
	DeckEditor.deckcolor.Visible = false
	DeckEditor.deck2.Visible = false
	DeckEditor.deck3.Visible = false
	DeckEditor.deck4.Visible = false
	DeckEditor.Color.Black.Visible = false
	DeckEditor.Color.White.Visible = false
	DeckEditor.Color.Pink.Visible = false
	DeckEditor.Color.DarkBlue.Visible = false
	DeckEditor.Color.LightBlue.Visible = false
	DeckEditor.Color.Purple.Visible = false
	DeckEditor.Color.Red.Visible = false
	DeckEditor.Color.Orange.Visible = false
	DeckEditor.Color.Green.Visible = false
	DeckEditor.Color.Yellow.Visible = false
	main = true
	blur.Size = 15
end
local function editor()
	mode = 'edit'
	Bar.Editor.Visible = true
	Bar.Deck.Visible = false
	Bar.Wheels.Visible = false
	Bar.Trucks.Visible = false
	Bar.MainMenu.Visible = false
	Bar.Back.Visible = true
	MainMenu.EditorButton.Visible = false
	MainMenu.PlayButton.Visible = false
	MainEditor.deckButton.Visible = true
	MainEditor.truckButton.Visible = true
	MainEditor.wheelButton.Visible = true
	WheelEditor.wheelblack.Visible = false
	WheelEditor.wheelgray.Visible = false
	WheelEditor.wheelwhite.Visible = false
	TruckEditor.truckblack.Visible = false
	TruckEditor.truckgray.Visible = false
	TruckEditor.truckwhite.Visible = false
	TruckEditor.trucknormal.Visible = false
	DeckEditor.deckcolor.Visible = false
	DeckEditor.deck2.Visible = false
	DeckEditor.deck3.Visible = false
	DeckEditor.deck4.Visible = false
	DeckEditor.Color.Black.Visible = false
	DeckEditor.Color.White.Visible = false
	DeckEditor.Color.Pink.Visible = false
	DeckEditor.Color.DarkBlue.Visible = false
	DeckEditor.Color.LightBlue.Visible = false
	DeckEditor.Color.Purple.Visible = false
	DeckEditor.Color.Red.Visible = false
	DeckEditor.Color.Orange.Visible = false
	DeckEditor.Color.Green.Visible = false
	DeckEditor.Color.Yellow.Visible = false
end

Currently i am doing this type of function around 6 times and it will probably grow in the future, really want an easier / more modular way of doing this cause currently i am needing to add all the ui elements i dont want in to the existing functions and its just yuck. Thanks in advance!

local function SetVisible(Parents, Visible)
    Parents = typeof(Parents) ~= "table" and {Parents} or Parents

    for One, Parent in ipairs(Parents) do
        for Two, Gui in ipairs(Parent:GetChildren()) do
            if Gui:IsA("GuiObject") then
                 Gui.Visible = Visible
            end
        end
    end
end

local function mainmenu()
    mode = 'main'

    SetVisible({Bar, MainEditor, WheelEditor, TruckEditor, DeckEditor, DeckEditor.Color}, false)
    SetVisible(MainMenu, true)
    Bar.MainMenu.Visible = true

    main = true
    blur.Size = 15
end

local function editor()
    mode = 'edit'

    SetVisible({Bar, MainMenu, WheelEditor, TruckEditor, DeckEditor, DeckEditor.Color}, false)
    SetVisible(MainEditor, true)
    Bar.Editor.Visible = true
    Bar.Back.Visible = true
end

not sure what your workspace looks like, but you should definitely utilize for loops when doing this type of stuff

basically SetVisible has two parameters
Parent/Parents and Visible
and it will set all the children’s Visible properties to Visible(if the child is a GuiObject)
also the children are the children of the Parents

hope this helps

2 Likes

try something like this

local thing = script.Parent --this is just for example
for i,v in pairs(thing:GetDescendants()) do --descendants is **Everything** inside of "thing", even all the children of the thing's children (if that makes sense)
if v:IsA("Frame") then --IsA("whatever you want to detect here")
v.Visible = false --true or false
end
end