Help with Modularizing My Roblox Script

Hey, I have a script that controls UI buttons, camera transitions, and UI management, but I want to refactor it into a more modular format to make it easier to maintain and expand. Can anyone help me break it down into separate modules? I’d really appreciate the help!"

Script

here-

print('THE TIME HAS STARTED')
wait(3)
print('THE TIME HAS ENDED')
-- Get the ScreenGui and MenuHolder
local mainMenuGui = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("MainMenuGui")
local menuHolder = mainMenuGui:WaitForChild("MenuHolder")

-- List of button names
local buttonNames = {"CustomizationSelect", "PlaySelect", "HelpSelect", "SettingsSelect", "CharacterSelect", "ShopSelect"}

-- Function to scale and rotate button on hover
local function onHover(button)
	button:TweenSize(UDim2.new(0.879,0,0.23,0), "Out", "Quad", 0.2, true) -- Slightly larger size
	local randomRotation = math.random(-15, 15)
	button.Rotation = randomRotation
end

-- Function to reset button size and rotation on hover off
local function onHoverOff(button)
	button:TweenSize(UDim2.new(0.779,0,0.17,0), "Out", "Quad", 0.2, true) -- Original size
	button.Rotation = 0
end

-- Function to reset button size and rotation on click
local function onClick(button)
	button:TweenSize(UDim2.new(0, 268, 0, 61), "Out", "Quad", 0.2, true) -- Original size
	button.Rotation = 0
end

-- Connect the hover, hover off, and click functions to each button
for _, buttonName in ipairs(buttonNames) do
	local button = menuHolder:WaitForChild(buttonName)
	button.MouseEnter:Connect(function()
		onHover(button)
	end)
	button.MouseLeave:Connect(function()
		onHoverOff(button)
	end)
	button.MouseButton1Click:Connect(function()
		onClick(button)
	end)
end

-- Get the Camera and the MainMenuCam part
local camera = game.Workspace.CurrentCamera
local mainMenuCam = game.Workspace:WaitForChild("MainMenuCam")
local customizationCam = game.Workspace:WaitForChild("CustomizationCam")
local settingsCam = game.Workspace:WaitForChild("SettingsCam")
local shopCam = game.Workspace:WaitForChild("ShopCam")
local helpCam = game.Workspace:WaitForChild("HelpCam")
local tvPlayCam = game.Workspace:WaitForChild("TVPlayCam")

-- Function to attach the camera to the MainMenuCam part
local function attachCamera()
	camera.CameraType = Enum.CameraType.Scriptable
	camera.CFrame = mainMenuCam.CFrame
end

-- Attach the camera when the script runs
attachCamera()

-- Optional: Keep the camera attached to MainMenuCam's position if it moves
mainMenuCam:GetPropertyChangedSignal("CFrame"):Connect(function()
	camera.CFrame = mainMenuCam.CFrame
end)

-- Function to tween the camera to a specified part
local function tweenCameraTo(part)
	local tweenService = game:GetService("TweenService")
	local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
	local goal = {CFrame = part.CFrame}
	local tween = tweenService:Create(camera, tweenInfo, goal)
	tween:Play()
	return tween
end

-- Play animations on SecurityManager
local securityManager = game.Workspace:WaitForChild("SecurityManager")
local turnAroundAnim = securityManager:WaitForChild("SecurityTurnAround")
local mainLoopAnim = securityManager:WaitForChild("SecurityMainLoop")
local animator = securityManager:FindFirstChildOfClass("Animator") or securityManager:WaitForChild("Humanoid"):WaitForChild("Animator")

local turnAroundTrack = animator:LoadAnimation(turnAroundAnim)
local mainLoopTrack = animator:LoadAnimation(mainLoopAnim)

-- Function to stop the main loop animation
local function stopMainLoop()
	if mainLoopTrack.IsPlaying then
		mainLoopTrack:Stop()
	end
end

-- Connect the buttons to tween the camera to the respective parts and stop the main loop animation
local customizationSelectButton = menuHolder:WaitForChild("CustomizationSelect")
customizationSelectButton.MouseButton1Click:Connect(function()
	tweenCameraTo(customizationCam)
	stopMainLoop()
	onClick(customizationSelectButton)

	-- Enable CustomizationTab and disable MainMenuGui
	local customizationTab = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("CustomizationTab")
	customizationTab.Enabled = true
	mainMenuGui.Enabled = false
end)

local settingsSelectButton = menuHolder:WaitForChild("SettingsSelect")
settingsSelectButton.MouseButton1Click:Connect(function()
	local tween = tweenCameraTo(settingsCam)
	stopMainLoop()
	onClick(settingsSelectButton)

	-- Disable MainMenuGui when tweening to SettingsCam
	mainMenuGui.Enabled = false

	-- Enable SettingsDefault when tweening to SettingsCam
	tween.Completed:Connect(function()
		local settingsDefaultGui = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("SettingsDefault")
		settingsDefaultGui.Enabled = true
	end)
end)

local shopSelectButton = menuHolder:WaitForChild("ShopSelect")
shopSelectButton.MouseButton1Click:Connect(function()
	tweenCameraTo(shopCam)
	stopMainLoop()
	onClick(shopSelectButton)
end)

local helpSelectButton = menuHolder:WaitForChild("HelpSelect")
helpSelectButton.MouseButton1Click:Connect(function()
	tweenCameraTo(helpCam)
	onClick(helpSelectButton)

	-- Enable HelpTab and disable MainMenuGui
	local helpTab = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("HelpTab")
	local AlijahTab = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("AlijahTab")
	helpTab.Enabled = true
	AlijahTab.Enabled = true
	mainMenuGui.Enabled = false
end)


-- Connect the PlaySelect button to tween the camera to TVPlayCam and enable TVPlayMenuGui after 3 seconds
local playSelectButton = menuHolder:WaitForChild("PlaySelect")
playSelectButton.MouseButton1Click:Connect(function()
	local tween = tweenCameraTo(tvPlayCam)
	stopMainLoop()
	onClick(playSelectButton)
	mainMenuGui.Enabled = false

	tween.Completed:Connect(function()
		wait(3)
		local tvPlayMenuGui = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("TVPlayMenuGui")
		tvPlayMenuGui.Enabled = true
	end)
end)

-- Connect the BackSelect button to return to the main menu
local backSelectButton = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("CustomizationTab"):WaitForChild("BackSelect")
backSelectButton.MouseButton1Click:Connect(function()
	tweenCameraTo(mainMenuCam)

	-- Enable MainMenuGui and disable CustomizationTab
	local customizationTab = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("CustomizationTab")
	customizationTab.Enabled = false
	mainMenuGui.Enabled = true
end)

-- Get the KeybindSelect button
local keybindPart = game.Workspace:WaitForChild("KeybindPart")
local keyBindsSurfaceGui = keybindPart:WaitForChild("KeyBindsSurfaceGui")
local keybindSelectButton = keyBindsSurfaceGui:WaitForChild("KeybindSelect")

-- Function to handle KeybindSelect button click
keybindSelectButton.MouseButton1Click:Connect(function()
	-- Enable FolderKeybindsGUI and disable MainMenuGui
	local folderKeybindsGui = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("FolderKeybindsGUI")
	folderKeybindsGui.Enabled = true
	mainMenuGui.Enabled = false

	-- Disable SettingsDefault when KeybindSelect is clicked
	local settingsDefaultGui = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("SettingsDefault")
	settingsDefaultGui.Enabled = false
end)
--Keybind script ends here

--Volume script starts here 
local volumePart = game.Workspace:WaitForChild("VolumePart")
local volumeSurfaceGui = volumePart:WaitForChild("VolumeSurfaceGui")
local volumeSelectButton = volumeSurfaceGui:WaitForChild("VolumeSelect")

-- Function to handle VolumwSelect button click
volumeSelectButton.MouseButton1Click:Connect(function()
	-- Enable FolderVolumeGUI and disable MainMenuGui
	local foldervolumeGui = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("FolderVolumeGUI")
	foldervolumeGui.Enabled = true
	mainMenuGui.Enabled = false

	-- Disable SettingsDefault when VolumeSelect is clicked
	local settingsDefaultGui = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("SettingsDefault")
	settingsDefaultGui.Enabled = false
end)
--Volume script ends here

--Graphics script starts here 
local graphicPart = game.Workspace:WaitForChild("GraphicPart")
local graphicSurfaceGui = graphicPart:WaitForChild("GraphicsSurfaceGui")
local graphicSelectButton = graphicSurfaceGui:WaitForChild("GraphicsSelect")

-- Function to handle GraphicsSelect button click
graphicSelectButton.MouseButton1Click:Connect(function()
	-- Enable FolderGraphicGUI and disable MainMenuGui
	local foldergraphicGui = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("FolderGraphicsGUI")
	foldergraphicGui.Enabled = true
	mainMenuGui.Enabled = false

	-- Disable SettingsDefault when GraphicSelect is clicked
	local settingsDefaultGui = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("SettingsDefault")
	settingsDefaultGui.Enabled = false
end)
--Graphic script ends here

--Badges script starts here 
local badgesPart = game.Workspace:WaitForChild("BadgePart")
local badgesSurfaceGui = badgesPart:WaitForChild("BadgesSurfaceGui")
local badgesSelectButton = badgesSurfaceGui:WaitForChild("BadgesSelect")

-- Function to handle BadgesSelect button click
badgesSelectButton.MouseButton1Click:Connect(function()
	-- Enable FolderBadgesGUI and disable MainMenuGui
	local folderbadgesGui = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("FolderBadgeGUI")
	folderbadgesGui.Enabled = true
	mainMenuGui.Enabled = false

	-- Disable SettingsDefault when BadgesSelect is clicked
	local settingsDefaultGui = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("SettingsDefault")
	settingsDefaultGui.Enabled = false
end)
--Badges script ends here
  • Also I have this script inside a ScreenGui called MainMenuGui

Here’s a start.

game.ReplicatedStorage.ButtonModule:

-- Function to scale and rotate button on hover
local function onHover(button)
	button:TweenSize(UDim2.new(0.879,0,0.23,0), "Out", "Quad", 0.2, true) -- Slightly larger size
	local randomRotation = math.random(-15, 15)
	button.Rotation = randomRotation
end

-- Function to reset button size and rotation on hover off
local function onHoverOff(button)
	button:TweenSize(UDim2.new(0.779,0,0.17,0), "Out", "Quad", 0.2, true) -- Original size
	button.Rotation = 0
end

-- Function to reset button size and rotation on click
local function onClick(button)
	button:TweenSize(UDim2.new(0, 268, 0, 61), "Out", "Quad", 0.2, true) -- Original size
	button.Rotation = 0
end

return function(button)
	button.MouseEnter:Connect(function()
		onHover(button)
	end)
	button.MouseLeave:Connect(function()
		onHoverOff(button)
	end)
	button.MouseButton1Click:Connect(function()
		onClick(button)
	end)
end

And then in your script:

local ButtonModule = require(game.ReplicatedStorage.ButtonModule)
-- Connect the hover, hover off, and click functions to each button
for _, buttonName in ipairs(buttonNames) do
	local button = menuHolder:WaitForChild(buttonName)
	ButtonModule(button)
end
1 Like

Thanks, I got it fixed I ended up creating a new local script and just called the functions that I created in the script above into my newly created script.

i liked the old one now it feels worse

I kept the old one I just reused the functions from them.

That’s bad

This text will be blurred

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.