Need help with a keycode open and closing gui

Hi so I have a round gui and I am having problems to be closed even i the gui open tween is not done if thats the case why the problem accours.

Basicly i want so when you press Q in this case the gui shall open and when the key goes up its shall close but what the problem is that if you click/tap and dont hold it will stay open when it shall close

Example When i am holing the q button: Closes when Q key goes up
((This is what i want but when i dont hold the Q button just tap/Click it basicly))
https://gyazo.com/b6e0dd5a699bb8360914f29e143398de

Example When I tap/click the q button: Stays open ((need to tap again to close))
https://gyazo.com/378b2864b5e99906c034b823a99ed4c6

The Code

local UserInputSevice = game:GetService("UserInputService")


UserInputSevice.InputBegan:Connect(function(InputKey)
	if InputKey.KeyCode == Enum.KeyCode.Q then
		Gui.Main:TweenSize(UDim2.new(0.330, 0, 0.600, 0), nil, nil, 0.5)
	end
end)

UserInputSevice.InputEnded:Connect(function(InputKey)
	if InputKey.KeyCode == Enum.KeyCode.Q then
		Gui.Main:TweenSize(UDim2.new(0, 0, 0, 0), nil, nil, 0.5)
	end
end)
1 Like

I would try something like this where you add an additional variable called holding

local UserInputSevice = game:GetService("UserInputService")
local holding = false
local open = false

UserInputSevice.InputBegan:Connect(function(InputKey)
	if InputKey.KeyCode == Enum.KeyCode.Q then
		if open then
			holding = false
			Gui.Main:TweenSize(UDim2.new(0.330, 0, 0.600, 0), nil, nil, 0.5)
			wait(0.1)
			holding = true
		else
			open = false 
			holding = false
			Gui.Main:TweenSize(UDim2.new(0, 0, 0, 0), nil, nil, 0.5)
		end
	end
end)

UserInputSevice.InputEnded:Connect(function(InputKey)
	if InputKey.KeyCode == Enum.KeyCode.Q then
		if holding then
			open = false
			Gui.Main:TweenSize(UDim2.new(0, 0, 0, 0), nil, nil, 0.5)
		end
	end
end)

Thank u that worked didnt do as u did but used ur idea

1 Like