Measure time between button being pressed and unpressed in ContextActionService

How would I measure the time between the inputState going from begin to end (essentially how long the button is held)

local function EvasiveAndTransportLogic(_input: string, inputState: Enum.UserInputState, _inputObject: InputObject)
	if _input == "EvasiveAndTransportLogic" and inputState == Enum.UserInputState.Begin and humanoid then
		-- Use the user's evasive move when the user activates their evasive move
		local playerEvasive = humanoid:GetAttribute("CurrentEvasive")
		local evasiveFolder = script.Parent.Evasives
		
		--where do i even start
		
		-- if tap then
			require(evasiveFolder[playerEvasive]):Perform(character)
		--elseif hold then
			--do whatever
		--end
	end
end

You can use tick():

local buttonHoldStartTime = nil 

local function EvasiveAndTransportLogic(_input: string, inputState: Enum.UserInputState, _inputObject: InputObject)
    if _input == "EvasiveAndTransportLogic" then
        if inputState == Enum.UserInputState.Begin then
            buttonHoldStartTime = tick()
        elseif inputState == Enum.UserInputState.End then
            if buttonHoldStartTime then
                local holdDuration = tick() - buttonHoldStartTime
                print(holdDuration)
                buttonHoldStartTime = nil
            end
        end
    end
end
1 Like

ain’t that deprecated (or at least supposed to be)

If you don’t wanna use it then use os.time() instead, should work the same

1 Like

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