Ad
Log in to vote
1

How to fix GUI input keycode changes its visiblility?

Asked by
TechModel 118
1 year ago

This is a local script that is in parent with with a frame and now I wanted to add a keyinput that if I press M then it closes or opens the GUI, but it doesnt work. The mouse button click works fine but the key input doesn't.

local map = script.Parent.Parent.MinimapFrame
local button = script.Parent

button.MouseButton1Click:Connect(function()
    if map.Visible == true then
        map.Visible = false
        button.Text = "Open Map"
    else
        map.Visible = true
        button.Text = "Close Map"
    end
end)

local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)

    if input.UserInputType == Enum.UserInputType.Keyboard then

        if input.KeyCode == Enum.KeyCode.M then
            if map.Visible = true then
                then map.Visible = false
else if map.Visible = false then
        map.Visible = true
    end
            end
        end
    end
end)

2 answers

Log in to vote
1
Answered by 1 year ago

try this

local map = script.Parent.Parent:WaitForChild("MinimapFrame")
local button = script.Parent

button.MouseButton1Click:Connect(function()
    if map.Visible == true then
        map.Visible = false
        button.Text = "Open Map"
    else
        map.Visible = true
        button.Text = "Close Map"
    end
end)

local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    if not gameProcessedEvent then
        if input.UserInputType == Enum.UserInputType.KeyBoard then
            if input.KeyCode == Enum.KeyCode.M then
                map.Visible = not map.Visible
            end
        end
    end
end)

if that doesn't work try this

local map = script.Parent.Parent:WaitForChild("MinimapFrame")
local button = script.Parent

button.MouseButton1Click:Connect(function()
    if map.Visible == true then
        map.Visible = false
        button.Text = "Open Map"
    else
        map.Visible = true
        button.Text = "Close Map"
    end
end)

local mouse = game.Players.LocalPlayer:GetMouse()

mouse.KeyDown:Connect(function(input)
    if input == "M" then
        map.Visible = not map.Visible
    end
end)

if that doesn't work too then dm me on discord (??#8344) (copy paste the invisible block because I have invisible name very sorry)

0
Thanks for accepting my answer! WINDOWS10XPRO 437 — 1y
0
No worries, glad that you could help me. TechModel 118 — 1y
Ad
Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

First of all, in your if/then statements you need == instead of just =.

Second, at line 22 you have an extra then.

Fixed code would look like this:

local map = script.Parent.Parent.MinimapFrame
local button = script.Parent

button.MouseButton1Click:Connect(function()
if map.Visible == true then
   map.Visible = false
   button.Text = "Open Map"
else
   map.Visible = true
   button.Text = "Close Map"
end
end)

local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)

if input.UserInputType == Enum.UserInputType.Keyboard then

   if input.KeyCode == Enum.KeyCode.M then
      if map.Visible == true then
         map.Visible = false
      else if map.Visible == false then
         map.Visible = true
      end
   end
end
end
end)

Answer this question