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