Holding or Clicking?

How would i find the difference between a click and a hold?

I came up with this solution. But it only checks when you release the mouse button.

And i want to do something while you are holding.

local uis = game:GetService("UserInputService")
local holdTime = 0

uis.InputBegan:Connect(function(input, event)
	if not event then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			holdTime = time()
		end
	end
end)

uis.InputEnded:Connect(function(input, event)
	if not event then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			local diff = time() - holdTime
			
			if diff > 0.1 then
				print("You were holding")
			else
				print("That was a click")
			end
		end
	end
end)
uis.InputBegan:Connect(function(input, event)
	if not event then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
            print("Button Down")
			holdTime = time()
		end
	end
end)

No. That would also fire when clicking. I only want to do it when you are holding (Not clicking).

1 Like
local UIS = game:GetService("UserInputService")

local currentTick = tick()

UIS.InputBegan:Connect(function(inp,gpe)
	if gpe then return end
	
	if inp.UserInputType == Enum.UserInputType.MouseButton1 then
		currentTick = tick()
	end
end)

UIS.InputEnded:Connect(function(inp)
	if inp.UserInputType == Enum.UserInputType.MouseButton1 then
		if tick() - currentTick < 0.1 then
			print("Click")
		else
			print("Hold")
		end
	end
end)

That is the same solution on my post.

oh then

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(inp,gpe)
	if gpe then return end

	if inp.UserInputType == Enum.UserInputType.MouseButton1 then
		repeat task.wait()
			print("Hold")
		until UIS:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) == false
	end
end)

That still fires when you click.

Solved.

local uis = game:GetService("UserInputService")
local holding = false
local holdTime = 0

uis.InputBegan:Connect(function(input, event)
	if not event then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then			
			holding = true
			holdTime = time()

			task.delay(0.1, function()
				if holding then
					print("Holding")
				else
					print("Click")
				end
			end)
		end
	end
end)

uis.InputEnded:Connect(function(input, event)
	if not event then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			holding = false

			local diff = time() - holdTime

			if diff > 0.1 then
				print("You were holding")
			else
				print("That was a click")
			end
		end
	end
end)
2 Likes
local userInput = game:GetService("UserInputService")

userInput.InputBegan:Connect(function(startKey, processed)
	if processed then return end
	local startTime = tick()
	local connection
	connection = userInput.InputEnded:Connect(function(endKey, processed)
		if processed then return end
		if startKey.KeyCode == endKey.KeyCode then
			local endTime = tick()
			local diffTime = endTime - startTime
			if diffTime <= 0.1 then
				print("Click!")
			else
				print("Hold!")
			end
			connection:Disconnect()
		end
	end)
end)

Just tested and this is working for multiple concurrent key presses.