Hello! I was wondering how to make the M key in my keyboard get you back to the main menu of the game. I tried using two scripts for this. Script #1: (Disables Main MenuGui, with music, blur effect, and camera if you click on Play (In this case my game is an FPS, so it's a Deploy Button))
-- Variables local player = game.Players.LocalPlayer local character = player.CharacterAdded local mouse = player:GetMouse() local camera = game.Workspace.Camera local defualtCFrame = camera.CFrame local view = 150 local blur = game.Lighting.Blur blur.Size = 11 -- Scripting in Action function updateCamera() camera.CFrame = game.Workspace.MenuCamPart.CFrame end game:GetService("RunService").RenderStepped:Connect(updateCamera) script.Parent.MenuFrame.DeployButton.MouseButton1Click:Connect(function() wait(0.2) blur.Size = 0 game.Workspace.MenuMusic:Stop() camera.CameraType = Enum.CameraType.Custom script.Parent.MenuFrame.Visible = false script.Parent.AntiCameraScript.Disabled = false -- Script #2 script.Disabled = true -- Script #1 end)
Then I have Script #2 (Re-enables Main MenuGui, with music, blur effect, and camera if you press the "M" key on your keyboard)
-- Variables local player = game.Players.LocalPlayer local character = player.CharacterAdded local UIS = game:GetService("UserInputService") local camera = game.Workspace.Camera local defualtCFrame = camera.CFrame local view = 150 local blur = game.Lighting.Blur -- Scripting in Action local function onInputBegan(input, gameProcessed) if input.UserInputType == Enum.KeyCode.M then wait(0.2) blur.Size = 11 game.Workspace.MenuMusic:Play() camera.CameraType = Enum.CameraType.Custom script.Parent.MenuFrame.Visible = true script.Parent.CameraScript.Disabled = false script.Disabled = true end end UIS.InputBegan:Connect(onInputBegan)
Script #1 works perfectly fine, but Script #2 doesn't. Would someone like to help me? Thanks, I'll be waiting for your answers!
Ah, your 2nd script is comparing UserInputState to KeyCode (both are different). It must be:
input.KeyCode = Enum.KeyCode.M
Also, here's the code in one script with some fixes. You don't need two of them.
-- Variables local player = game.Players.LocalPlayer local character = player.CharacterAdded local UIS = game:GetService("UserInputService") local mouse = player:GetMouse() local camera = game.Workspace.Camera local defualtCFrame = camera.CFrame local view = 150 local blur = game.Lighting.Blur local menuMusic = game.Workspace.MenuMusic local menuFrame = script.Parent.MenuFrame local menuOn = false -- Scripting in Action local connection function updateCamera() camera.CFrame = game.Workspace.MenuCamPart.CFrame end local function onInputBegan(input : InputObject, gameProcessed) if (input.KeyCode == Enum.KeyCode.M) and not menuOn then wait(0.2) blur.Size = 11 menuMusic:Play() camera.CameraType = Enum.CameraType.Scriptable -- You might need this to be set to scriptable so you can change the CFrame property of the camera. menuFrame.Visible = true -- Set camera connection connection = game:GetService("RunService").RenderStepped:Connect(updateCamera) menuOn = true end end script.Parent.MenuFrame.DeployButton.MouseButton1Click:Connect(function() wait(0.2) blur.Size = 0 menuMusic:Stop() camera.CameraType = Enum.CameraType.Custom menuFrame.Visible = false -- Disconnect camera connection:Disconnect() menuOn = false end) UIS.InputBegan:Connect(onInputBegan)
The reason of why it does not work, it's because the function does not get the variables called input
and gameProcessed
, giving them a nil value.
You should activate the function inside the event function, like this(copy & paste):
-- Variables local player = game.Players.LocalPlayer local character = player.CharacterAdded local UIS = game:GetService("UserInputService") local camera = game.Workspace.Camera local defualtCFrame = camera.CFrame local view = 150 local blur = game.Lighting.Blur -- Scripting in Action local function onInputBegan(input, gameProcessed) if input.UserInputType == Enum.KeyCode.M then wait(0.2) blur.Size = 11 game.Workspace.MenuMusic:Play() camera.CameraType = Enum.CameraType.Custom script.Parent.MenuFrame.Visible = true script.Parent.CameraScript.Disabled = false script.Disabled = true end end UIS.InputBegan:Connect(function(input, gameProcessed) -- gets the values you missed onInputBegan(input, gameProcessed) -- just adding this will fix your problem. end)