Need help fixing my main menu script (camera & userinput)

ISSUES:

  • When I press a key on my keyboard, it doesn’t do anything. Also, sometimes when I move my mouse out the screen it randomly transitions into game camera. I want it to stay in the menu until the player inputs any key.
  • The camera bug at the end, i’m trying to make it go back to it’s original position for the roblox character.

SCRIPT:

local StarterGui = game:GetService("StarterGui")
local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")

-- CAMERA SETTINGS
local cam = workspace.CurrentCamera

if not cam then
	warn("CurrentCamera - NOT FOUND")
	return
end

local camPart = game.Workspace:WaitForChild("CameraPart")
local mouse = game:GetService("Players").LocalPlayer:GetMouse()
local maxTilt = 10
local isMenuActive = true

-- GETTING THE PLAYER PROPERTIES
local LocalPlayer = game.Players.LocalPlayer
local PlayerGui = LocalPlayer:WaitForChild("PlayerGui")
local mmInterface = PlayerGui:WaitForChild("mainMenuInterface")
local character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = character:WaitForChild("Humanoid")

-- BILLBOARD GUI ELEMENTS
local SurfaceGUI = game.Workspace.mainMenuGUI:WaitForChild("monitorPart"):FindFirstChild("SurfaceGui")
local vidFrame = SurfaceGUI.VideoFrame
local gameTitle = SurfaceGUI.ImageLabel

-- SCREEN GUI'S
local sgUI = mmInterface.startingGameUI
local gCover = mmInterface.gameCover

-- VARIABLES FOR STARTING GAME UI
local bgUI = sgUI.backgroundUI
local disclaimer = sgUI.disclaimer
local hpAdvised = sgUI.headphonesAdvised
local pRoblox = sgUI.poweredByRoblox
local veilHorrorGames = sgUI.veilHorrorGames
local glitchEffect = sgUI.GlitchEffect

-- VARIABLES FOR GAME COVER
local fadeSlide = gCover.fadeTransition
local pressAnyButton = gCover.pressAnyButton

-- MAIN MENU SOUNDS
local mmMusic = mmInterface.mainMenuMusic
local fireSFX = mmInterface.fireCracklingSFX
local hpSound = hpAdvised.noiseCheck

repeat
	wait()
	cam.CameraType = Enum.CameraType.Scriptable
until cam.CameraType == Enum.CameraType.Scriptable

local connection
local function startCameraMovement()
	connection = game:GetService("RunService").RenderStepped:Connect(function()
		local mouse = game.Players.LocalPlayer:GetMouse()
		cam.CFrame = camPart.CFrame * CFrame.Angles(
			math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),
			math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),
			0
		)
	end)
end

local function stopCameraMovement()
	if connection then
		connection:Disconnect()
	end
	cam.CameraType = Enum.CameraType.Scriptable
	task.wait(0.5)
	cam.CameraType = Enum.CameraType.Custom
end


local function fadeIn(imageLabel, duration)
	imageLabel.Visible = true
	imageLabel.ImageTransparency = 1
	local tweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
	local tween = TweenService:Create(imageLabel, tweenInfo, { ImageTransparency = 0 })
	tween:Play()
	return tween
end

local function fadeOut(imageLabel, duration)
	local tweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
	local tween = TweenService:Create(imageLabel, tweenInfo, { ImageTransparency = 1 })
	tween:Play()
	tween.Completed:Wait()
	imageLabel.Visible = false
end

local function onInput()
	stopCameraMovement()
	task.wait(1)
	cam.CameraType = Enum.CameraType.Custom
	cam.CameraSubject = Humanoid -- Focus back on the player
	isMenuActive = false
	vidFrame.Playing = false
	mmMusic.Parent = game.StarterGui
	mmInterface:Destroy()
	
	UserInputService.InputEnded:Connect(onInput)
end

function playerMainMenu()
	fireSFX:Play()

	-- VeilHorror Games Presents:

	task.wait(1)
	fadeIn(veilHorrorGames, 2)
	task.wait(5)
	fadeOut(veilHorrorGames, 3)

	-- Powered by ROBLOX:

	task.wait(0.5)
	fadeIn(pRoblox, 2)
	task.wait(2)
	fadeOut(pRoblox, 2)

	-- Headphones Advised:

	task.wait(1)
	fadeIn(hpAdvised, 2)
	task.wait(2)
	hpSound:Play()
	task.wait(4)
	fadeOut(hpAdvised, 2)

	-- Disclaimer:

	task.wait(2)
	fadeIn(disclaimer, 2)
	mmMusic:Play()
	task.wait(5)
	fadeOut(disclaimer, 4)

	-- GAME COVER STARTS
	startCameraMovement()
	fadeOut(bgUI, 1)
	glitchEffect.Visible = false
	glitchEffect.Playing = false
	vidFrame.Visible = true
	vidFrame.Playing = true
	fireSFX:Stop()
	task.wait(2)
	fadeIn(pressAnyButton, 2)
	fadeIn(gameTitle, 2)
	task.wait(5)
end


playerMainMenu()
UserInputService.InputBegan:Connect(onInput)