Can't press the same button to open and close a UI

I’m trying to create a interaction menu that when you press “F” it will open, and if you press it again it will close. I tried making a true/false variable with the “CanOpen” but once I close it, the menu won’t open.

local UIS = game:GetService("UserInputService")

local NormalSize = UDim2.new(0.844, 0, 0.195, 0)
local HoverSize = UDim2.new(0.996, 0, 0.207, 0)

local ListFrame = script.Parent:WaitForChild("HolderFrame")

local InventoryButton = ListFrame:WaitForChild("InventoryButton")
local InventoryButtonVis = UDim2.new(0.5, 0, 0.145, 0)
local InventoryButtonInvis = UDim2.new(-0.526, 0, 0.145, 0)

local PartyButton = ListFrame:WaitForChild("PartyButton")
local PartyButtonVis = UDim2.new(0.5, 0, 0.381, 0)
local PartyButtonInvis = UDim2.new(-0.526, 0, 0.381, 0)

local SettingsButton = ListFrame:WaitForChild("SettingsButton")
local SettingsButtonVis = UDim2.new(0.5, 0, 0.616, 0)
local SettingsButtonInvis = UDim2.new(-0.526, 0, 0.616, 0)

local ReportButton = ListFrame:WaitForChild("ReportButton")
local ReportButtonVis = UDim2.new(0.5, 0, 0.851, 0)
local ReportButtonInvis = UDim2.new(-0.526, 0, 0.851, 0)

local CanOpen = true

UIS.InputBegan:Connect(function(Input, Typing)
	if Input.KeyCode == Enum.KeyCode.F and CanOpen == true and not Typing then
		CanOpen = false
		task.wait()
		InventoryButton:TweenPosition(
			InventoryButtonVis,
			"In",
			"Sine",
			0.25,
			false
		)
		task.wait(0.1)
		PartyButton:TweenPosition(
			PartyButtonVis,
			"In",
			"Sine",
			0.25,
			false
		)
		task.wait(0.1)
		SettingsButton:TweenPosition(
			SettingsButtonVis,
			"In",
			"Sine",
			0.25,
			false
		)
		task.wait(0.1)
		ReportButton:TweenPosition(
			ReportButtonVis,
			"In",
			"Sine",
			0.25,
			false
		)
	end
end)

UIS.InputBegan:Connect(function(Input, Typing)
	if Input.KeyCode == Enum.KeyCode.F and CanOpen == false and not Typing then
		CanOpen = true
		task.wait()
		InventoryButton:TweenPosition(
			InventoryButtonInvis,
			"In",
			"Sine",
			0.25,
			false
		)
		task.wait(0.1)
		PartyButton:TweenPosition(
			PartyButtonInvis,
			"In",
			"Sine",
			0.25,
			false
		)
		task.wait(0.1)
		SettingsButton:TweenPosition(
			SettingsButtonInvis,
			"In",
			"Sine",
			0.25,
			false
		)
		task.wait(0.1)
		ReportButton:TweenPosition(
			ReportButtonInvis,
			"In",
			"Sine",
			0.25,
			false
		)
	end
end)

Not sure if this will be of any use, but rather than creating two event listeners, use one with an elseif.

Eg.

UIS.InputBegan:Connect(function(Input, Typing)
	if Input.KeyCode == Enum.KeyCode.F and CanOpen == true and not Typing then
		CanOpen = false
		task.wait()
		InventoryButton:TweenPosition(
			InventoryButtonVis,
			"In",
			"Sine",
			0.25,
			false
		)
		task.wait(0.1)
		PartyButton:TweenPosition(
			PartyButtonVis,
			"In",
			"Sine",
			0.25,
			false
		)
		task.wait(0.1)
		SettingsButton:TweenPosition(
			SettingsButtonVis,
			"In",
			"Sine",
			0.25,
			false
		)
		task.wait(0.1)
		ReportButton:TweenPosition(
			ReportButtonVis,
			"In",
			"Sine",
			0.25,
			false
		)
	elseif Input.KeyCode == Enum.KeyCode.F and CanOpen == false and not Typing then
CanOpen = true
		task.wait()
		InventoryButton:TweenPosition(
			InventoryButtonInvis,
			"In",
			"Sine",
			0.25,
			false
		)
		task.wait(0.1)
		PartyButton:TweenPosition(
			PartyButtonInvis,
			"In",
			"Sine",
			0.25,
			false
		)
		task.wait(0.1)
		SettingsButton:TweenPosition(
			SettingsButtonInvis,
			"In",
			"Sine",
			0.25,
			false
		)
		task.wait(0.1)
		ReportButton:TweenPosition(
			ReportButtonInvis,
			"In",
			"Sine",
			0.25,
			false
		)
	end
end)

Yeahhh I tried my best re-writing it in the block stuff but I’m not really the best without the red lines guiding me 0_o

2 Likes

This fixed it, I don’t know why I immediately went to having 2 different listeners. Thank you.

3 Likes

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