Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
NGC4637 602 Moderation Voter
3 years ago
Edited 3 years ago

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?

0
I supposedly planned for it to be E but it had the same problem so I switched to F but the problem persisted NGC4637 602 — 3y
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. Ziffixture 6913 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

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.

0
Ziffixture answered my problem in the comments. Btw it was already a PlayerGui NGC4637 602 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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.

Answer this question