Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to fix GUI input keycode changes its visiblility?

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

✓ Accepted answer

#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)

2 comments

0

Thanks for accepting my answer!

0

No worries, glad that you could help me.

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.