I am writing the code for my main menu, and everything is working fine. However, the code just seems, well, messy to me. I was wondering what I could do to make this code cleaner? A couple of important things:
-
I have 2 modules: = One is a MiscFunctions module, which has things like GetDescendantsOfName, GetChildrenOfName, etc. The other one is the GuiFunctions module, which has some basic stuff to make the code a little cleaner. I don’t have all of my functions in that module because some of the functions need to access UI outside of it’s parent, and I don’t want to Ctrl-C Ctrl-V variables.
-
I have looked into object oriented programming, but I don’t know if this would be a good use for it, because I have a lot of different needs for my UI, and I know it is bad to overuse it. I don’t know if making a ton of different classes is a good idea. If this is a good use for it, please tell me!
local RepStore = game:GetService("ReplicatedStorage") -- Services
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local MiscFunctions = require(RepStore.Modules:FindFirstChild("MiscFunctions"))
local GuiFunctions = require(script:FindFirstChild("GuiFunctions"))
local Player = Players.LocalPlayer -- "Big" Vars
local PlayerGui = Player:WaitForChild("PlayerGui")
local Homescreen = PlayerGui:WaitForChild("Homescreen") -- Gui vars
local Info = PlayerGui:WaitForChild("Info")
local Announcement = PlayerGui:WaitForChild("Announcement")
local MainMenu = Homescreen:WaitForChild("MainMenu")
local Loadout = Homescreen:WaitForChild("Loadout")
local Shop = Homescreen:WaitForChild("Shop")
local OtherEvents = MiscFunctions.ConvertToDictionary(RepStore.OtherEvents:GetChildren()) -- Other Vars
local function MainMenuButton(button) -- Functions
GuiFunctions.TextHovered(button)
button.MouseButton1Click:Connect(function()
if button.Name == "Play" then
OtherEvents["Play"]:FireServer()
elseif button.Name == "Loadout" then
MainMenu.Visible = false
Loadout.Visible = true
else
MainMenu.Visible = false
Shop.Visible = true
end
end)
end
local function ExitButton(exitbutton)
GuiFunctions.ExitButtonHovered(exitbutton)
exitbutton.MouseButton1Click:Connect(function()
exitbutton.Parent.Visible = false
MainMenu.Visible = true
end)
end
local function UpdateText(property)
local Text = Info.Frame:WaitForChild(property.Name, true)
property:GetPropertyChangedSignal("Value"):Connect(function()
Text.Text = property.Value
end)
end
for i, button in ipairs(MiscFunctions.GetChildrenOfType(MainMenu, {"ImageButton"})) do -- Main Menu Functions
MainMenuButton(button)
end
for i, exitbutton in ipairs(MiscFunctions.GetDescendantsOfName(Homescreen, {"Exit"})) do -- Exit Buttons
ExitButton(exitbutton)
end
for i, button in ipairs(MiscFunctions.GetDescendantsOfName(Homescreen, {"ShopItem"})) do -- Shop item effect and click
GuiFunctions.ShopItemHovered(button)
button.MouseButton1Click:Connect(function()
OtherEvents["RequestToEquip"]:FireServer()
end)
end
for i, property in ipairs(RepStore.GuiProps:GetChildren()) do -- Update text properties and do announcement tween
if property.Name == "Map" or property.Name == "Music" then
UpdateText(property)
else
local FrameToTween = Announcement:WaitForChild("Frame")
local AnnouncementTweenInfo = TweenInfo.new(
.8,
Enum.EasingStyle.Cubic,
Enum.EasingDirection.In,
0,
false,
0
)
local TweenDB = false
local TweenOut = TweenService:Create(FrameToTween, AnnouncementTweenInfo, {Position = UDim2.new(.5, 0, 0, 0)})
local TweenIn = TweenService:Create(FrameToTween, AnnouncementTweenInfo, {Position = UDim2.new(.5, 0, -.3, 0)})
property:GetPropertyChangedSignal("Value"):Connect(function()
if TweenDB or MainMenu.Visible or Loadout.Visible or Shop.Visible then return end
TweenDB = true
Announcement.Frame.Background:WaitForChild("AnnouncementText").Text = property.Value
TweenOut:Play()
wait(3)
TweenIn:Play()
local CompletedConnection
CompletedConnection = TweenIn.Completed:Connect(function()
TweenDB = false
CompletedConnection:Disconnect()
end)
end)
end
end
Thanks!