Roblox Mouse Click, Holding Help

Hey guys, does anyone know how to detect if a player holds Left or Right Click on mouse? I’m trying to make a combact system, Long Hold for Heavy Attacks and Fast Clicks for Light Attacks, Any help will work :pray:

This is the code i currently got

local plr = game.Players.LocalPlayer
local userinputservice = game:GetService("UserInputService")



userinputservice.InputBegan:Connect(function(input, ret)
	
	if ret then
		return
	else
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			print('Left Click')
		end
		if input.UserInputType == Enum.UserInputType.MouseButton2 then
			print('Right Click')
		end
	end
end)```
4 Likes

just like this one, it also has a counterpart called InputEnded which checks if you let go of a keycode that a previous inputBegan registered, it’s pretty much the same so you won’t have any issues understanding it

local Mouse = game:GetService("Players").LocalPlayer:GetMouse()

Mouse.Button1Down:Connect(function()
	start = tick()
end)

Mouse.Button1Up:Connect(function()
	local duration = tick() - start
	print("Player held left click for: " .. duration)
	if duration > 3 then
		warn("Player held left click for over 3 seconds.")
	end
end)

There is probably a much better way to do this with UserInputService, however this worked fine for me.

Just use mouse.Button1Down like this:

local mouse = game.Players.LocalPlayer:GetMouse()

local uis = game:GetService("UserInputService")

uis.InputChanged:Connect(function(input, gpe)
       if input.InputType ==Enum.InputType.Keyboardthen
                  while mouse.Button1Down do
                        --Do stuff
                end
         end
end)
1 Like

You can detect if the player clocked and trigger a loop, then when inputended stop the loop.

local Mouse = game.Players.LocalPlayer:GetMouse()

local Holding = false 

Mouse.Button1Down:Connect(function()
	Holding = true 
	print(Holding)
end)

Mouse.Button1Up:Connect(function()
	Holding = false 
	print(Holding)
end)
1 Like

Just use while game.Players.LocalPlayer:GetMouse().MouseButton1Down do

This will always evaluate to true, Button1Down is an event, not a bool that defines whether the mouse is pressed down

And mouse MouseButton1Down isn’t even a thing for the mouse object

1 Like
local plr = game.Players.LocalPlayer
local uis = game:GetService("UserInputService")
local startLeftClick, startRightClick, endLeftClick, endRightClick = nil, nil, nil, nil

uis.InputBegan:Connect(function(input, processed)
	if processed then
		return
	end
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		startLeftClick = tick()
	elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
		startRightClick = tick()
	end
end)

uis.InputEnded:Connect(function(input, processed)
	if processed then
		return
	end
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		endLeftClick = tick()
		print("Left click held for "..endLeftClick - startLeftClick.." seconds.")
	elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
		endRightClick = tick()
		print("Right click held for "..endRightClick - startRightClick.." seconds.")
	end
end)

image

1 Like
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local startLeftClick, startRightClick, endLeftClick, endRightClick = nil, nil, nil, nil

mouse.Button1Down:Connect(function()
	startLeftClick = tick()
end)

mouse.Button2Down:Connect(function()
	startRightClick = tick()
end)

mouse.Button1Up:Connect(function()
	endLeftClick = tick()
	print("Left click held for "..endLeftClick - startLeftClick.." seconds.")
end)

mouse.Button2Up:Connect(function()
	endRightClick = tick()
	print("Right click held for "..endRightClick - startRightClick.." seconds.")
end)

With the mouse object instead of UserInputService.

1 Like

Yeah it is…? Mouse | Roblox Creator Documentation