How can i optimize this script?

What i’m trying to do is a simple ‘pad’ menu where if you press a textbutton or just an app icon, an entry (or frame) of selected character frame becomes visible while making sure everything is invisible, the script works fine but however it is very long considering there are over 7+ characters in the game, it is also very inefficient, is there any ways i could optimize and make the script shorter?:
Here it is:

local openfurnaily = script.Parent.OpenFruanily
local scren = script.Parent
local furnailyentry = script.Parent.Furnaily
local risytalentry = script.Parent.Risytal
local risytal = script.Parent.OpenRisytal
local comi = script.Parent.OpenComi
local comientry = script.Parent.Comi
openfurnaily.MouseButton1Click:Connect(function()
	scren.Transparency = 1
	risytalentry.Visible = false
	furnailyentry.Visible = true -- Furnaily Entry is visible and everything else is invisible
	openfurnaily.Visible = false
	risytal.Visible = false
	comi.Visible = false
	comientry.Visible = false 
end)

risytal.MouseButton1Click:Connect(function()
	scren.Transparency = 1
	risytalentry.Visible = true -- Risytal Entry is visible and everything else is invisible
	furnailyentry.Visible = false
	openfurnaily.Visible = false
	risytal.Visible = false
	comi.Visible = false
	comientry.Visible = false
end)
comi.MouseButton1Click:Connect(function()
	scren.Transparency = 1
	risytalentry.Visible = false
	furnailyentry.Visible = false
	openfurnaily.Visible = false
	comi.Visible = false
	comientry.Visible = true -- Comi Entry is visible and everything else is invisible
	risytal.Visible = false
end)

You can use a table to define each Character’s entry and Open UI objects, and iterate through them to run a function.

local scren = script.Parent

local chars = {
	Furnaily = {Open=scren.OpenFruanily, Entry=scren.Furnaily},
	Risytal = {Open=scren.OpenRisytal, Entry=scren.Risytal},
	Comi = {Open=scren.OpenComi, Entry=scren.Comi}
}

function setvisible(character:string)
	scren.Transparency = 1
	
	for k,t in pairs(chars) do
		t.Open.Visible = false
		if k == character then t.Entry.Visible = true continue end
		t.Entry.Visible = false
	end
end

function setmouseclick1()
	for name,ui in pairs(chars) do
		ui.Open.MouseButton1Click:Connect(function()
			setvisible(name)
		end)
	end
end

setmouseclick1()

It worked thank you! however will it also work in a similar script like this?

local close = script.Parent.TextButton -- This is found inside the character entry frame
local frame = script.Parent -- This is the character entry
local screen = script.Parent.Parent
local button = script.Parent.Parent.OpenFruanily
local button1 = script.Parent.Parent.OpenRisytal
local button2 = script.Parent.Parent.OpenComi
close.MouseButton1Click:Connect(function()
	frame.Visible = false -- It becomes invisible after clicking "GO BACK" on the character entry then making everything else including buttons visible
	screen.Transparency = 0
	button.Visible = true
	button1.Visible = true
	button2.Visible = true
end)

It works very similar. But you must redefine the table and what the function does. From what it looks like you’re just making one entry-frame for the three characters? instead of one for each. If that’s the case then it looks something like this:

local close = script.Parent.TextButton
local frame = script.Parent
local screen = script.Parent.Parent

local chars = {
	Fruanily = {Button = screen.OpenFruanily, Info = 'something'},
	Risytal = {Button = screen.OpenRisytal, Info = 'something'},
	Comi = {Button = screen.OpenComi, Info = 'something'}
}

function loadcharacter(charname:string) --an example of character-specific stuff
	
	frame.Title.Text = charname
	frame.Description.Text = chars[charname].Info
	--load stuff from the character to the gui or something like that
end

function setmouseclick1()
	for name,ui in pairs(chars) do
		ui.Button.MouseButton1Click:Connect(function()

			screen.Transparency = 1

			--turn off the buttons?
			for _,v in pairs(chars) do v.Button.Visible = false end
			
			--you can do whatever else here
			loadcharacter(name)
		end)
	end
end

close.MouseButton1Click:Connect(function()
	frame.Visible = false
	screen.Transparency = 0
	
	--make the buttons visible again
	for _,v in pairs(chars) do v.Button.Visible = true end
end)

setmouseclick1()

From what it looks like you’re just making one entry-frame for the three characters?

Yes each character has it’s own separate entry here is an example screenshot:


Pressing on the GO BACK button leads back to the screen menu where the app icons (or buttons to open the entries) is found.