Feedback on main menu script

So I made this main Menu script for the first time. I would like some feedback on what I should improve or change next time in the future I make another one

The script:

-- Variables
local ts = game:GetService("TweenService")
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

local camera = game.Workspace.CurrentCamera
local menuGui = player.PlayerGui:WaitForChild("MenuScreen")

local menuFrame = menuGui.BackFrame
local infoFrame = menuGui.InfoFrame
local creditsFrame = menuGui.CreditsFrame

local playButton = menuFrame.PLAY
local infoButton = menuFrame.INFO
local creditsButton = menuFrame.CREDITS

menuFrame.Visible = true
camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = game.Workspace.CameraPart.CFrame
hum.WalkSpeed = 0

local regularSize = UDim2.new(0.189, 0,0.083, 0)
local hoverSize = UDim2.new(0.203, 0,0.093, 0)

local startPos = UDim2.new(0.274, 0,-0.7, 0)
local endPos = UDim2.new(0.274, 0,0.157, 0)

local debounce = false

-- Tables
local buttons = {
	{button = playButton},
	{button = infoButton},
	{button = creditsButton}
}

local frames = {
	{frame = infoFrame, Framebutton = infoButton},
	{frame = creditsFrame, Framebutton = creditsButton}
}

local tweenInfoHover = TweenInfo.new(
	.2,
	Enum.EasingStyle.Quad,
	Enum.EasingDirection.InOut
)

local tweenInfoFrame = TweenInfo.new(
	.7,
	Enum.EasingStyle.Quad,
	Enum.EasingDirection.InOut
)

local tweenInfoFade = TweenInfo.new(
	2,
	Enum.EasingStyle.Quad,
	Enum.EasingDirection.Out
)

-- Playbutton onclick event
playButton.MouseButton1Click:Connect(function()
	playButton.Visible = false
	infoButton.Visible = false
	creditsButton.Visible = false
	camera.CameraType = Enum.CameraType.Custom
	hum.WalkSpeed = 16
	
	local tween = ts:Create(menuFrame, tweenInfoFade, {Transparency = 0})
	tween:Play()
	tween.Completed:Wait()
	
	ts:Create(menuFrame, tweenInfoFade, {Transparency = 1}):Play()
end)

-- Other buttons onclick event for the frames
for _,v in frames do
	v.Framebutton.MouseButton1Click:Connect(function()
		if v.frame.Visible == false and not debounce then
			debounce = true
			v.frame.Position = startPos
			v.frame.Visible = true
			local openTween = ts:Create(v.frame, tweenInfoFrame, {Position = endPos})
			openTween:Play()
			
			creditsButton.Visible = false
			infoButton.Visible = false
			playButton.Visible = false
			
			openTween.Completed:Wait()
			debounce = false
			end

		v.frame.Close.MouseButton1Click:Connect(function()
			if v.frame.Visible == true and not debounce then
				debounce = true
				creditsButton.Visible = true
				infoButton.Visible = true
				playButton.Visible = true
				
				local closeTween = ts:Create(v.frame, tweenInfoFrame, {Position = startPos})
				closeTween:Play()
				closeTween.Completed:Wait()
				v.frame.Position = endPos
				v.frame.Visible = false
				debounce = false
			end
		end)
	end)
end

-- Tweens the size of button whenever hovered over and tweens it back when not hovered anymore
for _,v in buttons do
	v.button.MouseEnter:Connect(function()
		ts:Create(v.button, tweenInfoHover, {Size = hoverSize}):Play()
		v.button.MouseLeave:Connect(function()
			ts:Create(v.button, tweenInfoHover, {Size = regularSize}):Play()
		end)
	end)
end
1 Like

Organization of variables: It is good practice to group all related variables at the beginning of the script. In your case you could group all GUI related variables (menuGui, menuFrame, infoFrame, creditsFrame) and all button related variables (playButton, infoButton, creditsButton) separately.

Variable naming: It is important to choose descriptive variable names to make the code easier to understand. For example, instead of ts, char, um, frames, it would be better to use names like tweenService, character, humanoid, menuFrames, respectively.

Comments: Adding explanatory comments to complex pieces of code can help you understand the purpose and function of certain sections.

Improve readability: To improve code readability, consider adding proper indentation and line spacing. This will make the code easier to read and understand.

Avoid code repetition: You have similar code snippets to handle button hovering. Instead of repeating the code, you can create a separate function to handle hovering buttons and call it for each button.

Dealing with debouncing: Using the debounce variable to prevent repeated clicks is a good practice. Make sure that debouncing is applied consistently across all relevant parts of the code.

Error Handling: It is important to add proper error handling to handle cases where expected objects like menuGui are not present or when errors occur during animations.

Iā€™m learning too, just started. Keep going, we can do it!

Thanks for the feedback, ill be sure to use it the next time some aspects of it

1 Like