How to make a key both open a gui and close it?

so im making a gui which you can open by pressing a keycode and i was wondering if there was a way to both open then close the gui with the keycode.

1 Like

Yes, there is.

local userinputService = game:GetService("UserInputService")
local Frame = script.Parent.Frame -- Location of your Frame/Button..


userinputService.InputBegan:Connect(function(Key,Processed)
	if Processed then return end
	if Key.KeyCode == Enum.KeyCode.R then -- Change 'R' to your KeyCode.
		Frame.Visible = not Frame.Visible
	end
end)
2 Likes

Hey, this works but iā€™m trying to make it tween back how do i do this?

For example Iā€™m making it so when it opens it tweens open and when it closes i want it to tween to close anyknow how to do this?

this should be able to help when it comes to tweening the gui GUI Animations

i know how to tween it its just i need to know when to tween it

local UIS = game:GetService(ā€œUserInputServiceā€)
local gui = game:GetService(ā€œStarterGuiā€)
local player = game.Players.LocalPlayer
local open = script.Parent.Parent.Value
open = true
local Frame = script.Parent

UIS.InputBegan:Connect(function (Input,Processed)
if Processed then return end
if Input.KeyCode == Enum.KeyCode.G then
script.Parent:TweenPosition(UDim2.new(0.123, 0,0.228, 0,ā€œOutā€,ā€œSineā€,3))
Frame.Visible = not Frame.Visible
end
end)

you could probably make it so it checks to see if itā€™s already open and if itā€™s already open it could tween it back

thats what im saying how would i do this

A trick I like to use is setting an attribute I name ā€œOpenā€ instead of using the visible property. This allows you to use GetAttributeChangedSignal to then tween the position when it changes. The benefit is that you can interrupt the tween without worrying about it becoming invisible partway through.

1 Like

sorry, im new to this whole roblox scripting thing could you maybe empathize this some more

wait, just searched up what attributes were on roblox dev i need to use a bool value right

yes a bool is what you will need to use

okay how would i use the bool value

Yes;
Essentially a menu is either open, or closed, therefore a boolean is the best way to toggle this state. However, if we just use visible and a tween, then you need to set visibility first or last depending on if its being opened, or closed, and thats annoying.

So instead, donā€™t use visibility and just tween them off the screen. To do this and keep its ā€˜current stateā€™, we can use a boolean attribute and toggle that value. When this value changes, we can then tween the frame to its respected position.

Here is something I wrote up real quick:

local TS = game:GetService("TweenService")
local tween = TweenInfo.new(.2, Enum.EasingStyle.Back, Enum.EasingDirection.Out)

local frame = script.Parent:WaitForChild("Frame")

local open_position = UDim2.new(.5,0,.5,0)
local closed_position = UDim2.new(.5,0,1.5,0)

local function updatePosition()
	local is_open = frame:GetAttribute("Open") == true -- makes nil false
	TS:Create(frame, tween, {
		Position = is_open and open_position or closed_position
	}):Play()
end

script.Parent:WaitForChild("Frame"):GetAttributeChangedSignal("Open"):Connect(updatePosition)
updatePosition()

script.Parent:WaitForChild("TextButton").MouseButton1Down:Connect(function()
	local is_open = frame:GetAttribute("Open") == true -- again, nil would be turned to false here
	frame:SetAttribute("Open", not is_open) -- if its open, we want it closed, if its closed, we want it open
end)

This is the result:
menutoggle
You can click it multiple times and it will interrupt itself without any issues.

1 Like

looks good but i think i did it better

not saying you did anything wrong

Its a proof of concept, you can change the tween how you like.

1 Like

heres result after messing around https://i.gyazo.com/6514634128d39a13418d43faf1d5433d.mp4

thank you for helping me im really stupid :frowning: