Noticable delay in UserInput system where you have to hold before it initiate action

Im making a UserInput system in a local script where player can, for the sake of the example, lets say they’re opening a map. And for you to do it, you have to hold the Q key for one second before the map is shown, and you still have to hold it to keep looking at the map. Then the map would dissapear after you release the Q key. And here is a system where im checking whenever player is holding the Q key button during the initial 1 second delay before the map actually open.

The problem is that, while the code is successful and work fine. The thing is, there’s a noticable delay before the code actually run and open the map., essentially make you feel like you have to press for more than one second before the map can actually open. Sometimes the delay vary, sometimes for a whole 2 seconds.

I pointed the 3 delay location in the script below.

So im wondering what could cause the delay to happen. Is it because the checking system (checkKeyPressed()) is unoptimized or is there any reason the delay occur.

local ContextActionService = game:GetService("ContextActionService")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local function checkKeyPressed()
	-- This function is for checking whenever the keycode im aiming is pressed
	local keys = UserInputService:GetKeysPressed()
	local isPressing = false

	for i, key in pairs(keys) do -- Check if Button Q is pressed, Return true if do
		if key.KeyCode == Enum.KeyCode.Q then isPressing = true end
	end

	return isPressing
end

local ActionInitiated = false

local function KeybindPressed(actionName,inputState,inputObject)
	-- For PC only
	if inputState == Enum.UserInputState.Begin then -- On start holding
		
		local confirmTime = 0
		local success = true
		
		while confirmTime < 1 do -- This is to ensure the key is held for 1 second
			confirmTime += RunService.Heartbeat:Wait()

			if not checkKeyPressed() then 
				-- Constantly checking whenever Q is still being pressed (Held)
				-- If the key is released before the action can be initiated. Return success as false to skip the code below
				
				--[[
					The delay problem also occur here
				]]
				success = false 
				print("Prematurely released")
				break 
			end
		end

		if success then
			-- Do stuff if the button is successfully holded for one second without release
			
			--[[
			 The issue is heere. While the code do work and would successfully print the line below.
			 The problem lies with there's a noticeable increase in time it need to actually run the code.
			 Essentially, you would actually need to hold the Key for more than one second before it actually print.
			 The delay time vary and not constant.
			]]
			
			ActionInitiated = true
			print("Action initiated")
		end
	elseif Enum.UserInputState.End and ActionInitiated then -- On release. It doesnt matter whenever the code above is successfully performed or not, as there's alreaady a special check for it
		ActionInitiated = false
		
		-- The delay also occur here.
	end
end

ContextActionService:BindAction("Action",KeybindPressed,false,Enum.KeyCode.Q)

Quick question, why are you using checkKeyPressed() function? Could you also use

if not UserInputService:IsKeyDown(Enum.KeyCode.Q) then

instead of

if not checkKeyPressed() then 

It could be because of the delay from looping through all of the keys. Also, maybe another delay is caused due to using renderstepped?

Edit: I meant heartbeat, sorry

1 Like

Huh? There is no RenderStepped anywhere in the script. I usually use heartbeat.

Also shoot, i forgot :IsKeyDown() exist. If i know about it. I wont go my way using specialized function for it.