Opening a gui using UserInputService

Hello people of the forums,
I’m trying to make a gui pop up when I press the P key on the keyboard, and close when I press P again. The issue is that when I do push it, the gui pops up, and closes itself imediately after I looked at other people that had the same problem, but I’m also tweening the gui as well, so I don’t know what to do.

Script (in the Frame

local tweenService = game.TweenService
local tweenInfo = TweenInfo.new(
	0.5,
	Enum.EasingStyle.Exponential,
	Enum.EasingDirection.InOut,
	0,
	false,
	0
)

local goal = {
	Size = UDim2.new(0.6,0,0.6,0),
	BackgroundColor3 = Color3.new(1, 1, 1)
}
local goal2 = {
	Size = UDim2.new(0,0,0,0),
	BackgroundColor3 = Color3.new(0, 0, 0)
}

local tween = tweenService:Create(script.Parent, tweenInfo, goal)
local tween2 = tweenService:Create(script.Parent, tweenInfo, goal2)


local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(imp, gpe)
	if gpe then return end
	
	if imp.KeyCode == Enum.KeyCode.P then
		if script.Parent.Visible == false then
			script.Parent.Visible = true
			tween:Play()
			tween.Completed:Wait()
		end
		if script.Parent.Visible == true then
			tween2:Play()
			tween.Completed:Wait()
			script.Parent.Visible = false
		end
	end
end)

Any help is appreciated, and thank you in addvance.

2 Likes

You have to return when the first conditional executes or use an else statement instead of two separated conditionals:

(Im writing this with my cellphone sorry)

It is doing what you told it to do..
An else or elseif is what you want here.

First if asks if it’s false, if so make it true. The next if asks if it’s true, if so make it false.
So if it enters false, it will leave false also. You don’t want to split that up like that.

if imp.KeyCode == Enum.KeyCode.P then
	if script.Parent.Visible == false then
		script.Parent.Visible = true
		tween:Play()
		tween.Completed:Wait()
	else
		tween2:Play()
		tween.Completed:Wait()
		script.Parent.Visible = false
	end
end

or

if imp.KeyCode == Enum.KeyCode.P then
	if script.Parent.Visible == false then
		script.Parent.Visible = true
		tween:Play()
		tween.Completed:Wait()
	elseif script.Parent.Visible == true then
		tween2:Play()
		tween.Completed:Wait()
		script.Parent.Visible = false
	end
end
1 Like

Change “if script.Parent.Visible == true then” to else if script.Parent.Visible == true then
Currently, your versoin changes the frame to visible and then immediatly after that checks again to see if its visible which is true and then closes.

It solves the problem but it only opens up once. If I try to open the gui again it just doesn’t.

1 Like

hey, idk if ur still having problems but this works for me. (also added a cooldown for when the animation finished but if u werent going for that feel free too delete)

local tweenService = game.TweenService
local tweenInfo = TweenInfo.new(
	0.5,
	Enum.EasingStyle.Exponential,
	Enum.EasingDirection.InOut,
	0,
	false,
	0
)

local goal = {
	Size = UDim2.new(0.6,0,0.6,0),
	BackgroundColor3 = Color3.new(1, 1, 1)
}

local goal2 = {
	Size = UDim2.new(0,0,0,0),
	BackgroundColor3 = Color3.new(0, 0, 0)
}

local tween = tweenService:Create(script.Parent, tweenInfo, goal)
local tween2 = tweenService:Create(script.Parent, tweenInfo, goal2)

local uis = game:GetService("UserInputService")
local cooldown = false

uis.InputBegan:Connect(function(imp, gpe)
	if gpe or cooldown then return end
	if imp.KeyCode == Enum.KeyCode.P then
		cooldown = true
		if script.Parent.Visible == false then
			script.Parent.Visible = true
			tween:Play()
			tween.Completed:Wait()
			cooldown = false
		else
			tween2:Play()
			tween2.Completed:Wait()
			script.Parent.Visible = false
			cooldown = false
		end
	end
end)
2 Likes

(hopefully it works and you didnt miss my 3 subtle edits lol)

2 Likes

what did you change tho? I am curious because I can’t tell what you did differently.

2 Likes

i meant 3 subtle edits to my post
But regarding your code im pretty sure i just added an elseif here:

if script.Parent.Visible == true then

changed this to a tween2 (you had this in the treu bit by accident)

tween.Completed:Wait()

and added the cooldown thingy

i think the accidental tween instead of tween2 was messing you up

2 Likes

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