How to make frame visible or invisible by pressing keyboard key?

i need to open or close gui by pressing a key, but it only closes and opens without second pressing

local u = game:GetService("UserInputService")

u.InputBegan:Connect(function(input) -- input is when you press a key
	if input.KeyCode == Enum.KeyCode.N then -- click N
		script.Parent.Frame.Visible = false
	else
		script.Parent.Frame.Visible = true
	end
end)
1 Like

Try reading the code out like it’s being explained.

If the input keycode is equal to N, then make Visible = false.
Otherwise (else), make Visible = true.
This means any other key will make it visible.

What you’re probably trying to achieve is:
If the input keycode is equal to N, then Visible = not .Visible

1 Like

I need to make it so that when the key is pressed for the first time, Frame becomes invisible and when the button N is pressed for the second time, Frame becomes visible

Yes, that is what I supplied you with. By using not, it’ll reverse the boolean of the current value of .Visible which will toggle it on and off.

how to use it in my script? Sorry for my misunderstanding

[quote=“damirandos, post:1, topic:2844709, username:damirandos”]

local u = game:GetService("UserInputService")
local on = false

u.InputBegan:Connect(function(input) -- input is when you press a key
	if input.KeyCode == Enum.KeyCode.N then -- click N
		on = not on -- if its true, its false. if its false, its true.
		script.Parent.Frame.Visible = on
	end
end)

the reason ur script isnt working is because ur putting else on the keycode statement that detects if ur pressing n… so instead make a boolean variable that is set to false first (or true if u want it on first) and use not to disable and enable it automatically…

3 Likes

Yep, this. You don’t need to define a variable though, just simply do script.Parent.Frame.Visible = not script.Parent.Frame.Visible.

2 Likes

yup this is better, just tried to add the variable for a little explaining. Thanks.

2 Likes

Thank you very much for this solution!

2 Likes

Thank you i finally understand how to do it!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.