How do I detect when someone is holding a key?

So I’m working on a project that involves keybinds. I am trying to detect when a player presses a key or holds a key it will tween the player camera fov. How would I detect if a player is holding a key down, and how would I make it so that while it’s holding it tweews the fov value up till it hits 120 or 1?

To get when the player presses the button, use:
https://developer.roblox.com/en-us/api-reference/event/UserInputService/InputBegan

To get when they release, use:
https://developer.roblox.com/en-us/api-reference/event/UserInputService/InputEnded

Putting together these puzzle pieces you get:


local UIS=game:GetService("UserInputService");

UIS.InputBegan:Connect(function(input)
    if input.KeyCode==Enum.KeyCode.E then
        print("Pressed button.")
        -- your code
    end
end)
 
UIS.InputEnded:Connect(function(input)
    if input.KeyCode==Enum.KeyCode.E then
        print("Released button.")
        -- your code 
    end
end)

4 Likes

It’s not printing released button, how do i fix this?