How could I shorten this script?

-- Script Variables --
local plr = game:GetService("Players").LocalPlayer
local plrGui = plr.PlayerGui
local replicatedStorage = game:GetService("ReplicatedStorage")
local guiManager = require(replicatedStorage.Managers.GuiManager)
local camManager = require(replicatedStorage.Managers.CameraManager)
local soundManager = require(replicatedStorage.Managers.SoundManager)

for _,child in pairs(plrGui:GetChildren()) do
	repeat wait() until child
end
									
-- Main Menu Buttons --
local mainMenu = plrGui.Main
local Play = mainMenu.Play.TextButton
local Tutorial = mainMenu.Tutorial.TextButton
local Settings = mainMenu.Settings.TextButton

-- Operator Menu Buttons --
local currentPage = plrGui.Operator.List.Content.UIPageLayout.CurrentPage
local operatorMenu = plrGui.Operator
local Back = operatorMenu.Back.TextButton
local PageForward = operatorMenu.List.Title.NextPage
local PageBack = operatorMenu.List.Title.PreviousPage

-- Runtime --
-- Play "Click" Sound When Button Pressed
for _,item in plrGui:GetDescendants() do
	if item.ClassName == "TextButton" then
		item.MouseButton1Click:Connect(function()
			if not item:GetAttribute("debounce") then
				soundManager:playSound("Button")
			end
		end)
	end
end

-- Manage Main Menu Buttons --
Play.MouseButton1Click:Connect(function() -- Play
	if Play:GetAttribute("debounce") == false then
		task.wait(0.001)
		Play:SetAttribute("debounce",true)

		warn(script.Name.. ": Play Pressed By " ..plr.Name)

		guiManager:transitionIn(1)
		camManager:setCam(Enum.CameraType.Scriptable, workspace.MenuArea.OperatorCamera)
		guiManager:setGui("Operator")
		guiManager:transitionOut(1)

		print(script.Name.. ": " ..plr.Name.. " Play Button Ran")
		Play:SetAttribute("debounce",false)
	end
end)

Tutorial.MouseButton1Click:Connect(function() -- Tutorial
	warn(script.Name.. ": Tutorial T.B.D")
end)

Settings.MouseButton1Click:Connect(function() -- Settings
	warn(script.Name.. ": Settings T.B.D")
end)

-- Manage Operator Menu Buttons --
Back.MouseButton1Click:Connect(function() -- Back
	if Back:GetAttribute("debounce") == false then
		task.wait(0.001)
		Back:SetAttribute("debounce",true)

		warn(script.Name.. ": Back Pressed By " ..plr.Name)

		guiManager:transitionIn(1)
		camManager:setCam(Enum.CameraType.Scriptable, workspace.MenuArea.MainCamera)
		guiManager:setGui("Main")
		guiManager:transitionOut(1)

		print(script.Name.. ": " ..plr.Name.. " Back Button Ran")
		Back:SetAttribute("debounce",false)
	end
end)

PageForward.MouseButton1Click:Connect(function() -- Page forward
	if PageForward:GetAttribute("debounce") == false then
		task.wait(0.001)
		PageForward:SetAttribute("debounce",true)

		local pageLayout = plrGui.Operator.List.Content.UIPageLayout

		pageLayout:Next()
		plrGui.Operator.List.Title.Title.Text = pageLayout.CurrentPage.Name

		task.wait(.25)
		print(script.Name.. ": " ..plr.Name.. " Paged Forward")
		PageForward:SetAttribute("debounce",false)
	end
end)

PageBack.MouseButton1Click:Connect(function() -- Page backward
	if PageBack:GetAttribute("debounce") == false then
		task.wait(0.001)
		PageBack:SetAttribute("debounce",true)

		local pageLayout = plrGui.Operator.List.Content.UIPageLayout

		pageLayout:Previous()
		plrGui.Operator.List.Title.Title.Text = pageLayout.CurrentPage.Name

		task.wait(.5)
		print(script.Name.. ": " ..plr.Name.. " Paged Backward")
		PageBack:SetAttribute("debounce",false)
	end
end)

currentPage.Changed:Connect(function()
	for _,item in currentPage:GetDescendants() do
		if item.ClassName == "TextButton" then
			item.MouseButton1Click:Connect(function()
				for _,operator in replicatedStorage.OperatorModels:GetDescendants() do
					if item.Parent.Name == operator.Name then
						print(item.Name)
						print(item:GetAttribute("Type"))
					end
				end
			end)
		end
	end
end)

I’m making a script that manages all of the game’s GUI buttons. It’s not completely developed but currently works as intended in the game I’m using it in. My only concern with it right now is length and being able to add more once my game becomes more complete. Some of it seems pretty repetitive. I know moduleScripts exist and I use them in my game for various purposes, but I’m not sure how I would go about using them here.

Any other improvements that could be made are also welcome, though my main focus right now is the length.

can’t you just paste the code here? how long is it?

I haven’t posted in this before so I just followed the instructions it said- but I edited the code into the post. I know it doesn’t seem extremely long but I feel like it’s repetitive or could be shortened some to make it easier to add on to later down the line.

1 Like

An easy way to shorten code is to make functions of code blocks you use repetitively.

you can make a local function of this.

2 Likes

you could also make a checkdebounce function so you wont have those extra 4 lines every times

1 Like

Should look like something like this

local function checkDebounce(button)
	if button:GetAttribute("debounce") == false then
		task.wait(0.001)
		button:SetAttribute("debounce",true)
	end
end

local function transitionInOut(camera)
	guiManager:transitionIn(1)
	camManager:setCam(Enum.CameraType.Scriptable, workspace.MenuArea[camera])
	guiManager:setGui(camera)
	guiManager:transitionOut(1)
end

Play.MouseButton1Click:Connect(function() -- Play
	checkDebounce(Play)

		warn(script.Name.. ": Play Pressed By " ..plr.Name)

		transitionInOut("Operator")

		print(script.Name.. ": " ..plr.Name.. " Play Button Ran")
		Play:SetAttribute("debounce",false)

end)
1 Like