How else can I organize this script?

Hi! So I have this script. It does work but I’m trying to find a way to shorten it.

The code works for a check-in GUI. When you click the check-in button, it disappears and other GUI text labels and buttons appear. Here is my code I provided inside the main frame:

local button = script.Parent.TextButton

local class1 = script.Parent["Seating Classes1"]
class1.Visible = false
local class2 = script.Parent["Seating Classes2"]
class2.Visible = false
local class3 = script.Parent["Seating Classes3"]
class3.Visible = false

local freeButton = script.Parent.Free
local groupButton = script.Parent.Group
local passButton = script.Parent.Pass

freeButton.Visible = false
groupButton.Visible = false
passButton.Visible = false

button.MouseButton1Click:Connect(function()
   button.Visible = false
   script.Parent.TextLabel:TweenSize(UDim2.new(0, 400, 0, 45), 0.75)
   wait(0.75)
   class1.Visible = true
   class2.Visible = true
   class3.Visible = true
   freeButton.Visible = true
   groupButton.Visible = true
   passButton.Visible = true
end)

I want to group the buttons into one but every time I do local buttons = {class1, class2, class3, freeButton, groupButton, passButton} in one line and put buttons.Visible at the next, nothing happens.

It would be very appreciating if you know how to shorten the script without using .Visible every time.

make a seperate function that can set the visibility of every instance in a table to a given value.

1 Like

When you do buttons.Visible, it doesn’t set every instance to that value, instead it would try to add “Visible” to the table and give it a value

1 Like

You’re on the right path, but you must iterate that table. Something like:

for _, v in pairs(buttons) do
    v.Visible = true
end
1 Like

When uniformly using the same statement repeatedly, write a function to do it for you and generalize it.

Try this:

local button = script.Parent.TextButton

-- "Class" Buttons
local class1 = script.Parent["Seating Classes1"]
local class2 = script.Parent["Seating Classes2"]
local class3 = script.Parent["Seating Classes3"]
-- "Other" Buttons
local freeButton = script.Parent.Free
local groupButton = script.Parent.Group
local passButton = script.Parent.Pass

function SetVisibleTo(Buttons, bool)
	for _, obj in ipairs(Buttons) do
		if obj.Visible ~= nil then
			obj.Visible = bool
		end 
	end
end

SetVisibleTo({class1, class2, class3, freeButton, groupButton, passButton}, false)

button.MouseButton1Click:Connect(function()
	button.Visible = false
	script.Parent.TextLabel:TweenSize(UDim2.new(0, 400, 0, 45), 0.75)
	wait(0.75)
	SetVisibleTo({class1, class2, class3, freeButton, groupButton, passButton}, true)
end)
1 Like

Thanks for your help. The “for” loop did the trick.