Hold key* to Enable UI

Hi, I am trying to make a custom Inventory that you need to hold Q to open it and Release Q to close it

I checked on forums, youtube, asked ai but couldnt find any that fits my expectation

So basically I am asking how to detect if the player holds Q

You can utilize both UserInputService.InputBegan and UserInputService.InputEnded signals to achieve this.

Something like this should work (assuming it’s a Frame).

local UserInputService = game:GetService("UserInputService")

local inventoryFrame = script.Parent -- Should be path to your inventory UI (should be a Frame)

UserInputService.InputBegan:Connect(function(input: InputObject, processed: boolean): ()
    if processed then return end
    if input.KeyCode ~= Enum.KeyCode.Q then return end

    inventoryFrame.Visible = true
end)
UserInputService.InputEnded:Connect(function(input: InputObject, processed: boolean): ()
    if processed then return end
    if input.KeyCode ~= Enum.KeyCode.Q then return end

    inventoryFrame.Visible = false
end)

Unfortunately it is not working with no errors or warns in output.

In which part of the code it checks if q is being hold ?

it is just a regular open close code if i am not wrong (it doesnt work this way either)

You made a small mistake here. It’s supposed to be

if input.KeyCode ~= Enum.KeyCode.Q then return end
1 Like

Yeah, that’s it, my bad. I’ll edit my original post. @Y_3ktn

1 Like

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