Detecting if a key is held or tapped

hey im trying to detecting if the G key is held or tapped using tick beetween input begains and receiving the signal from a playing annimation but i think i made a wrong turn somewhere but don’t really know when since it only prints “held” even with the slightest tap

local GPressed = nil

UserInputService.InputBegan:Connect(function(key, typing)
	if not typing then
		if key.KeyCode == Enum.KeyCode.G then

			ReversalRedAnimTrack:Play()
			GPressed = tick()
			task.wait(ReversalRedAnimTrack.Length - 0.1)
			ReversalRedAnimTrack:AdjustSpeed(0)
		end
	end
end)

ReversalRedAnimTrack:GetMarkerReachedSignal("Change"):Connect(function()
	if tick() - GPressed < 0.5 then
		--tapped
		print("tapped")
	else
		--held
		print("held")
	end
	GPressed = nil
end)```
1 Like

Tick doesn’t return decimals, so it being less than a 0.5 delay is impossible (unless it’s 0). Maybe try using RunService.Stepped and its “time” value it returns?

Shouldn’t you just use InputEnded to figure this out? If the input isn’t ended then perform your held code.

yes but i also need the code to run when the animation trigers the marker “Change”, thus why didn’t use input ended since the code would run when the G key is dropped and not when the annimation triggers the marker

1 Like

imma be honnest i have NO IDEA how to use runservice but from what i’ve seen i don’t think its addapted to the situation as said above i need to trigger or at least know if the G key is tapped or held when the markers triggers

update : found a solution by myself here is the code

local GPressed = 0
local EndGPressed = nil

UserInputService.InputBegan:Connect(function(key, typing)
	if not typing then
		if key.KeyCode == Enum.KeyCode.G then

			ReversalRedAnimTrack:Play()
			spawn(function()
				for i = 1, 10 do
					GPressed = GPressed + 0.1
					task.wait(0.1)
				end
			end)
			task.wait(ReversalRedAnimTrack.Length - 0.1)
			ReversalRedAnimTrack:AdjustSpeed(0)
		end
	end
end)

UserInputService.InputEnded:Connect(function(input, typing)
	if typing == false then
		if input.KeyCode == Enum.KeyCode.G then
			EndGPressed = GPressed
		end
	end
end)

function ReversalRedHeld()

end

ReversalRedAnimTrack:GetMarkerReachedSignal("Change"):Connect(function()
	if EndGPressed == nil then
		--still holding
	elseif EndGPressed < 0.7 then
		--tapped
	else
		--held
	end


	GPressed = 0
	EndGPressed = nil
end)```
1 Like

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