How do I detect if a player holds down a button for an x amount of time?

Hi, I’m vrs2210, and I have a problem with my userinputservice, I can’t seem to find out how tick() works and I can’t find any tutorials that explain this.

tick() records the amount of seconds since a certain date. When they press down, we can record how many seconds since tick() started. Then when they are done clicking we can measure the time between when they started and finished. This will tell you how long they held down the “E” key.

local UIS = game:GetService("UserInputService")

local lasttimeclicked  --variable, will use this later

UIS.InputBegan:Connect(function(input)
        if input.KeyCode == Enum.KeyCode.E then
                lasttimeclicked = tick() --detect what time they started holding down
        end
end)

UIS.InputEnded:Connect(function(input)
        if input.KeyCode == Enum.KeyCode.E then
                local timeheld = tick() - lasttimeclicked --subtract when they started from the current time
                print(timeheld.." seconds")
        end
end)
4 Likes

The thing is I want my script to activate after a set time while holding the button and not when the button is unpressed.

this is a local script

local key = "E" -- Replace with interact key.
local heldTime = 0
local neededTime = 5 -- This is in seconds
local holding = false
local finished = false

local UserInputService = game:GetService("UserInputService")
UserInputService .InputBegan:Connect(function(info)
     if info.KeyCode == Enum.KeyCode[key] then
          holding = true
          while holding do
               wait(1)
               heldTime = heldTime + 1
               if heldTime == neededTime then
                    finished = true
                    heldTime = 0
                    break
               end
          end
     end
end)

UserInputService.InputEnded:Connect(function(info)
     if info.KeyCode == Enum.KeyCode[key] then
          holding = false
          heldTime = 0
     end
end

This is an alternative to tick()

Yes I know this one, but I want to use tick for mine.

ok i think i can modify it to be tick()

I tested this and it works.

local key = "E" -- Replace with interact key.
local heldTime
local neededTime
local holding = false
local finished = false
local Time = 5 -- This is in seconds

local UserInputService = game:GetService("UserInputService")
UserInputService .InputBegan:Connect(function(info)
	if info.KeyCode == Enum.KeyCode[key] then
		holding = true
		neededTime = math.ceil(tick())
		heldTime = math.ceil(tick())
		while holding do
			heldTime = math.ceil(tick())
			if heldTime - neededTime >= Time then
				finished = true
	    		heldTime = nil
           		neededTime = nil
				break
			end
			wait(1)
		end
	end
end)

UserInputService.InputEnded:Connect(function(info)
	if info.KeyCode == Enum.KeyCode[key] then
		holding = false
		heldTime = nil
		neededTime = nil
	end
end)

I used math.ceil so it wouldn’t be 1611239033.5242, it would be 1611239033. (Example)

1 Like