keyHold function does not function

Hey devForum so I’m just working on an old project and I’ve never solved this problem and I still don’t know how to.

Basically the function is to fire when you’re holding down the key but It does not function. There aren’t no errors or anything, just an ineffective function.

  • Snippet
local keyHoldingEvent
local direction
local allKeysHeld = {}
local isKeyHoldRunning = false

local function keyHold()
	isKeyHoldRunning = true
	if isKeyHoldRunning then
		isKeyHoldRunning = true
		local validKeys = newKeybind.getKeys()

		local function validKeyPressed()
			local status = false

			for key,action in pairs(validKeys) do
				if allKeysHeld[Enum.KeyCode[key]] and not status then
					status = true
					print("hi")
					break
				end
			end
			return status
		end
		
		local function playLoopedAnimation()
			for key, action in pairs(validKeys) do
				if allKeysHeld[Enum.KeyCode[key]] then
					direction = getInputDir(Enum.KeyCode[key])
					if direction ~= nil and direction ~= "No key!" then
						local AssetId = "rbxassetid://" .. mic:GetAttribute(direction .. "Animation")
						local animation = playAnimation(AssetId)
						animation.Looped = true  -- Set the animation to loop
						animation:Play()
						print("hi")
					end
				end
			end
		end

		local function highlightHeldArrow()
			for key,action in pairs(validKeys) do
				if allKeysHeld[Enum.KeyCode[key]] then
					highlightImage(gui.Game.GlowArrows:FindFirstChild(action .. "Arrow"),"In")
					print("hi")
				else
					print("hi")
					highlightImage(gui.Game.GlowArrows:FindFirstChild(action .. "Arrow"),"Out")
				end
			end
		end
		if keyHoldingEvent then
			keyHoldingEvent = true
			task.spawn(function()
				while validKeyPressed() do
					task.wait()
					print("hi")
					highlightHeldArrow()
					playLoopedAnimation()
				end
				keyHoldingEvent = false
			end)
		end
	end
end

UIS.InputBegan:Connect(function(input, IsTyping)
	if micEquipped and not IsTyping and 
		(input.UserInputType == Enum.UserInputType.Keyboard) then

		local direction = getInputDir(input.KeyCode)
		print(direction)
		allKeysHeld[input.KeyCode] = true
		
		keyHold()
		if direction ~= nil and direction ~= "No key!" then
			local AssetId = "rbxassetid://" .. mic:GetAttribute(direction .. "Animation")
			highlightImage(gui.Game.GlowArrows:FindFirstChild(direction .. "Arrow"))
			playAnimation(AssetId)
			hudScale()
		end
	end
end)

UIS.InputEnded:Connect(function(input, IsTyping)
	if input.UserInputType == Enum.UserInputType.Keyboard and not IsTyping then
		allKeysHeld[input.KeyCode] = false
	end

	if animationTrack ~= nil then
		if animationTrack.IsPlaying then
			local onStop
			onStop = animationTrack.Stopped:Connect(function()
				onStop:Disconnect()
				--highlightImage(gui.Game.GlowArrows:FindFirstChild(direction .. "Arrow"),"Out")
			end)
		end
	end
end)

If you want the rest of the code so you have a more into detail of this problem, just ask please and Thank you.

Also, this project is Old so some of the code is very unorganized. Once I fix this problem I’ll make sure to fix that :wink: :joy:

i used inputbegin and inputend to detect key holding function for a sprint system