Keydown to open

Hello! I’ve been searching the web to find an updated way to open guis with a hotkey. I’m not sure what is wrong with this code if you can help me or correct my code fully, please do. Thank you for helping!

local plrGui = player:WaitForChild("PlayerGui"):WaitForChild("gui")
local inventory = plrGui.inventory
local userInput = game:GetService("UserInputService")

userInput.InputBegan:Connect(function(key)
	local open = false
	if key.KeyCode == Enum.KeyCode.E and open == false then
		print("pressed E")
		inventory:TweenPosition(UDim2.new(0.5,0,0.5,0), "In", "Bounce", 1)
		open = true
		
	elseif key.KeyCode == Enum.KeyCode.E and open == true then
		inventory:TweenPosition(UDim2.new(0.5,0,1.5,0), "Out", "Bounce", 1)
		open = false	
	end
end)
1 Like

Where does it error?
(30 characters)

You have to put open outside of the function, otherwise everytime the function is ran, open will be false

local open = false
userInput.InputBegan:Connect(function(key)
--code
end)

local variables within functions are not saved when changed after execution and will use the default values when ran again.

There are no errors in the output

I just added that, it doesn’t seem to have made a change because it doesn’t even print "pressed E" in the output

Is your code in a local script?

Yes, local script in StarterGui.

EDIT: moving into the actual Gui to see if it makes a difference.

I think the issue may be somewhere else, can you provide the whole script? When I tested the code, it works as intended.


local player = game.Players.LocalPlayer
local plrGui = player:WaitForChild("PlayerGui"):WaitForChild("gui")
local createButton = plrGui.contents.saves.slots.error:WaitForChild("create")
local loadButton = plrGui.contents.saves.slots.slot1:WaitForChild("load")
local inventory = plrGui.inventory
local items = plrGui.inventory.items
local count = items.count.Value
--local slots	= plrGui.contents.slots
local slots = plrGui.contents.saves.slots
local repStore = game:GetService("ReplicatedStorage")
local clientServer = repStore:WaitForChild("clientServer")
local serverClient = repStore:WaitForChild("serverClient")
local fileNum = 1
local userInput = game:GetService("UserInputService")

-- Below is dealing with the intro
createButton.MouseButton1Click:Connect(function()
	clientServer:FireServer("new")
end)

loadButton.MouseButton1Click:Connect(function()
	clientServer:FireServer("select", fileNum)
end)

-- Cutscene camera
--[[
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
camera.CameraSubject = workspace.startingCamera
camera.CFrame = workspace.startingCamera.CFrame
]]

-- make slot visible( intro aswell)
serverClient.OnClientEvent:Connect(function(msg, num)
	if msg == "createSlot" or msg == "slot1" then
		
		for i,options in pairs(slots:GetChildren()) do
			if options:IsA("GuiObject") then
					
				slots["slot"..num].Visible = true
				
			end
		end
	end
end)

-- update intro slots
while wait(.1) do
	if slots["slot1"].Visible == true and slots["slot2"].Visible == true and slots["slot3"].Visible == true then
		slots.error.Visible = false
	end
end

-- e to open Gui
local open = false
userInput.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.E and open == false then
		print("pressed E")
		inventory:TweenPosition(UDim2.new(0.5,0,0.5,0), "In", "Bounce", 1)
		open = true
		
	elseif key.KeyCode == Enum.KeyCode.E and open == true then
		inventory:TweenPosition(UDim2.new(0.5,0,1.5,0), "Out", "Bounce", 1)
		open = false
		
	end
end)


Your while wait(.1) is interrupting the thread. You have to place it at the very bottom of the script so all the event will have the chance to connect.

-- e to open Gui
local open = false
userInput.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.E and open == false then
		print("pressed E")
		inventory:TweenPosition(UDim2.new(0.5,0,0.5,0), "In", "Bounce", 1)
		open = true
		
	elseif key.KeyCode == Enum.KeyCode.E and open == true then
		inventory:TweenPosition(UDim2.new(0.5,0,1.5,0), "Out", "Bounce", 1)
		open = false
		
	end
end)

-- update intro slots
while wait(.1) do
	if slots["slot1"].Visible == true and slots["slot2"].Visible == true and slots["slot3"].Visible == true then
		slots.error.Visible = false
	end
end

Ohh! Thank you! I see what I did wrong.