How do I detect if a player has pressed a certain key for more than 1 second?

Like I want to check if a player was holding left shift for more than 1 second, thus enabling auto-sprint

2 Likes

Use proximity prompt make it transparent change the settings and put it in the player.

are there any alternatives to proximity prompt?

To do this you have two choices, you can either use:

https://developer.roblox.com/en-us/api-reference/class/ContextActionService

or

https://developer.roblox.com/en-us/api-reference/class/UserInputService

=> Check the button pressed => if shift => set walkspeed.

https://developer.roblox.com/en-us/api-reference/event/UserInputService/InputBegan
https://developer.roblox.com/en-us/api-reference/function/ContextActionService/BindAction

You could use DevBeat’s way but that wouldn’t count the time.

You can count the time yourself by using the time from when the input started and a loop. os.clock() can get you the current time, and you can constantly check if the current os.clock time - the start time is greater than 1

UserInputService should do the job, there was another article talking about

tick()

Visible Script Isn't Working - #8 by Minecrablox which comes in handy for time… But first here it is: [Moderate success]

local KeyPressed = Enum.KeyCode.LeftShift
local UIS = game:GetService("UserInputService")
if UIS.KeyboardEnabled then
    function InputBegan()
        if UIS:IsKeyDown(KeyPressed) then
            wait(.5)
            if UIS:IsKeyDown(KeyPressed) then
               wait(.5)
               if UIS:IsKeyDown(KeyPressed) then
                  -- code here
               end
            end
        end
    end
    UIS.InputBegan:Connect(InputBegan)
end
1 Like

Simply detect when stop pressing the key, and the make a value false, the value stop a while whit the condition of the value is true.

Use UIS.InputBegan and UIS.InputEnded to do this like this:

local UIS = game:GetService("UserInputService")
local key = Enum.KeyCode.LeftShift
local timer
local alrAct = false
UIS.InputBegan:Connect(function(input, gp)
        if gp return end
        if input.KeyCode == key and not alrAct then
           timer = os.clock()
           wait(1)
           if UIS:IsKeyDown(key) then
           -- keep sprint
              alrAct = true
           end
        end
end)

UIS.InputEnded:Connect(function(input, gp)
       if gp then return end
       if input.KeyCode == key then
          if (os.clock() - timer) >= 1 and not alrAct then
                --keep sprint
                alrAct = true
          end
       end
end)

By the way, you will need another code to deactivate alrAct.