Managed to condense 4 separate scripts into this one to manage my GUIs. Looked over it several times. Could anything be improved for readability/length/efficiency?
(This is a moduleScript in starterPlayerScripts, I use a module loader)
local ButtonHandler = {}
-- Script Variables --
local plr = game:GetService("Players").LocalPlayer
local modules = game:GetService("ReplicatedStorage").Modules
local camModule = require(modules.CameraModule)
local guiModule = require(modules.GuiModule)
local soundModule = require(modules.SoundModule)
local previewModule = require(modules.PreviewModule)
local connections = {}
-- Buttons --
local mainMenu = plr.PlayerGui.Main
local play = mainMenu.Play.Play
local tutorial = mainMenu.Tutorial.Tutorial
local settings = mainMenu.Settings.Settings
local operatorMenu = plr.PlayerGui.Operator
local pageLayout = operatorMenu.List.Content.UIPageLayout
local back = operatorMenu.Back.Back
local pageForward = operatorMenu.List.Title.NextPage
local pageBack = operatorMenu.List.Title.PreviousPage
local buttons = {play,tutorial,settings,back,pageForward,pageBack}
-- Functions --
local function checkDebounce(button)
-- Check Player's Debounce --
if button:GetAttribute("debounce") == false then
task.wait(0.001)
button:SetAttribute("debounce",true)
return true
else
return false
end
end
local function transitionTo(camera,gui)
-- Transition Player Into Gui/Camera --
guiModule:transitionIn(1)
camModule:setCam(Enum.CameraType.Scriptable, workspace.MenuArea[camera])
guiModule:setGui(gui)
guiModule:transitionOut(1)
end
local function pageChange(direction)
-- Change Page Forward Or Backward --
if direction == "Forward" then
pageLayout:Next()
elseif direction == "Backward" then
pageLayout:Previous()
else
warn(script.Name.. ": Invalid Page Direction!? Try again.")
end
task.wait(.25)
print(script.Name.. ": " ..plr.Name.. " Paged " ..direction)
end
local function buttonClicked(button)
-- Switch Case Depending On Button Pressed To Call Functions --
local buttons = {
["Play"] = function()
transitionTo("OperatorCamera","Operator")
end,
["Tutorial"] = function()
warn("Tutorial T.B.D")
end,
["Settings"] = function()
warn("Settings T.B.D")
end,
["Back"] = function()
transitionTo("MainCamera","Main")
end,
["NextPage"] = function()
pageChange("Forward")
task.wait(.25)
end,
["PreviousPage"] = function()
pageChange("Backward")
task.wait(.25)
end,
["default"] = function()
warn(script.Name.. ": Clicked button not found!?")
end,
}
if buttons[button.Name] then
buttons[button.Name]()
else
buttons["default"]()
end
end
function ButtonHandler:Init()
-- Manage Most Menu Buttons --
for _, button in buttons do
button.MouseButton1Click:Connect(function()
if not checkDebounce(button) then return end
soundModule:playSound("Button")
warn(script.Name.. ": " ..button.Name.. " Pressed By " ..plr.Name)
buttonClicked(button)
print(script.Name.. ": " ..plr.Name.. " " ..button.Name.. " Button Ran")
button:SetAttribute("debounce",false)
end)
end
-- Manage View / Spawn Buttons --
pageLayout.CurrentPage.Changed:Connect(function()
for _, connection in connections do
connection:Disconnect()
end
for _,item in pageLayout.CurrentPage:GetDescendants() do
-- Type Checks --
if item.ClassName ~= "TextButton" then continue end
if item:GetAttribute("Type") == nil then continue end
local connection = item.MouseButton1Click:Connect(function()
-- When View Button Pressed --
if item:GetAttribute("Type") == "View" then
warn(script.Name.. ": Operator " ..item.Name.. " View Button Pressed By " ..plr.Name)
previewModule:viewOperator(item)
-- When Spawn Button Pressed --
elseif item:GetAttribute("Type") == "Spawn" then
warn("Spawning T.B.D")
else
warn(script.Name.. ": Button type not found!?")
end
end)
table.insert(connections, connection)
end
end)
-- Manage Perk Buttons --
plr.PlayerGui.Operator.PerkList.Content.Frame.ChildAdded:Connect(function(button)
button.MouseButton1Click:Connect(function()
previewModule:viewPerk(button.Name)
end)
end)
end
return ButtonHandler