Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Wondering how to use UserInputService to make "M = Go back to MenuGui" ?

Asked by 2 years ago

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!

0
Did you mean blur.Size = Udim2.new(1,0,1,0)? or 11,11,11,11. AProgrammR 398 — 2y
0
No, I meant how to use the UserInputService to make "M" bring you back to the main menu of the game. isir2204 28 — 2y
0
Ok, when the event fires, do the same thing as you did when the menu pop up. Try share the code which makes the main menu visible and ill modify a little bit AProgrammR 398 — 2y
0
There is no event isir2204 28 — 2y

2 answers

Log in to vote
0
Answered by
Y_VRN 246 Moderation Voter
2 years ago

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)
0
Thank you so much! This script helped with the rest, but when I first join the game, I only see myself and not the scenery that I set my camera to record, but when I click deploy and I click "M" it does appear? Is there a solution to this? isir2204 28 — 2y
0
Oh wait I think I know why, it's because in line 19 there is nothing that defines the variable. what do i add there? the camera? isir2204 28 — 2y
0
Not sure what you meant by the scenery not showing up when you join first. When I tested this earlier, there were no problems. Can you try screen-recording what you are having problems with? By the way, the "connection" variable is meant to be empty, it is later added by onInputBegan() and then disconnected by pressing M. Y_VRN 246 — 2y
0
So basically, when I join the game, the camera does not look at what it's supposed to be looking at, instead, the player would see themselves, and not what the camera is seeing. isir2204 28 — 2y
View all comments (5 more)
0
If you're talking about having the menu show up as a player joins in, you simply have to have the menu visible already (Menu.Visible = true) before the game starts. Y_VRN 246 — 2y
0
No I meant like the camera holding what it's supposed to be recording in the background, how do I fix that? isir2204 28 — 2y
0
Are you saying that the camera doesn't show the background yet when you join? Like the menu is visible but the camera still isn't CFramed to the MenuCamPart? If that's the case, I'll edit the post with more modifications.. Y_VRN 246 — 2y
0
yes, that was my problem! isir2204 28 — 2y
0
Alright. Unfortunately, the website's acting up, so I can't edit here. I have the code on Pastebin though: https://pastebin.com/TsRX4HS8 Y_VRN 246 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

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)
0
It does not seem to work, there isn't even an output nor an error, i am confused. isir2204 28 — 2y
0
@isir2204 Your script must've not been parented on a valid area. If it is a LocalScript, place it on any object that is listed in this wiki link: https://developer.roblox.com/en-us/api-reference/class/LocalScript Y_VRN 246 — 2y
0
@Y_VRN Both scripts parented to a PlayerGui (i.e; in this case, a MenuGui), something that is listed in the wiki link, and it does not work. isir2204 28 — 2y

Answer this question