Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does the keyboard shortcut for my inventory GUI not work?

I have a script for my inventory GUI. it requires you to either press the Open/Close Inventory button or Press F. Below is a serverscript for the button

local frame = script.Parent.Parent.Inventory
local UserInputService = game:GetService("UserInputService")

function OpenOrClose()
	if frame.Visible == false then -- Checks to see if it is open or closed
		frame.Visible = true -- Opens the GUI
	elseif frame.Visible == true then -- Checks to see if it is open or closed
		frame.Visible = false -- Closes the GUI
	end
end

script.Parent.MouseButton1Click:Connect(function()
	if frame.Visible == false then
		frame.Visible = true 
	elseif frame.Visible == true then
		frame.Visible = false 
	end
end)

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input,gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.F and not gameProcessedEvent then
			OpenOrClose()
		end
	end
end)

The button works except when I press F, it doesn't open or close. Can anyone tell me why?

2 comments

0

I supposedly planned for it to be E but it had the same problem so I switched to F but the problem persisted

0

Serer-sided Scripts do not have access to Services that are designated on the Client's device. UserInputService is only accessible from LocalScripts for obvious reasons, you should also be programming a UI in a LocalScript, as again, it is locally designated.

2 answers

If i'm correct you need to set the Gui as the Player Gui.

I hope you know how to do that, before asking how to do that if you don't know search on google first for some tutorials.

If not comment down below and I'll do my best to help.

1 comment

0

Ziffixture answered my problem in the comments. Btw it was already a PlayerGui

The only possible way that I can see why your code doesn't work is that "UserInputService" only works in Localscripts. Most likely, you've written your code in a Serverscript. If this doesn't work, well then I don't know.

This discussion is preserved as it was. New answers, comments, and votes are closed.