How do I detect when a player is holding down their MouseButtoon?

Hello! I’m continuing to work on my mining system, I have written out this code and I’m doubting myself on how to decide whether or not the client is holding down their Left Mouse Button down and their mouse is on an ore. Here is the code I have written out, please help me out if you can! My goal is to make this the most efficient way possible, so if you can help me accomplish that, I’d appreciate it!

function Activated()
	local Target = mouse.Target.Parent.Name
	local Hitbox = mouse.Target.Parent
	if Target and workspace.SpawnedOres:FindFirstChild(Target) then
		local newPart = setTarget:InvokeServer(Target)
		local Gui = player:WaitForChild('PlayerGui'):WaitForChild('Gui')
		local Status = Gui:WaitForChild('Status')
		Status:TweenPosition(UDim2.new(.5,0,0.7,0), "In", "Bounce", .8)
		
		Hitbox.Damage.Changed:Connect(function()
			local newX = Hitbox.Damage.Value / Hitbox.Max.Value
			Status.Slider.Size = UDim2.new(newX,0,1,0)			
			checkComplete(Hitbox, Status)
		end)
		
		--[[ Commented out because I tried with this and it didn't work
		UserInputService.InputBegan:Connect(function(input)
		   local inputType = input.UserInputType
		   if inputType == Enum.UserInputType.MouseButton1 and Target then
			--	hold = true
				wait(config.Cooldown)
				Hitbox.Damage.Value = Hitbox.Damage.Value - config.Damage.Value
		    end
		end)    
		
		UserInputService.InputEnded:Connect(function(input)
			local inputType = input.UserInputType
			if inputType == Enum.UserInputType.MouseButton1 then
			--	hold = false
			end
		end)]]	
		
		mouse.Button1Down:Connect(function()
		--	if Target then      Commented out because I'm not sure if this is the right way to get what the Players Mouses' Target is.
			wait(config.Cooldown)
			Hitbox.Damage.Value = Hitbox.Damage.Value - config.Damage.Value
		--	end
		end)
		
	else
		print("Ore is not valid.")
	end
end
3 Likes

The BEST way of detecting is using ContextActionService. You can bind any input(s) to a single function.

Because you are trying to use if for a tool, I would recommend having an action bound when the tool is equpped, and immediately unbound when unequiped.

For Mouse Down, you would use Enum.UserInputType.MouseButton1.

3 Likes

Ohh! Okay, how do I detect if the mouse button is being held down?

1 Like

I would personally use a while loop and a variable. That would look something like this:

while true do
if YOUR_VARIABLE then
--Code
end
wait()
end

There could be many other ways to do this, but I went with the method that I would go with. This is also how I have been doing it.

How would this work with detecting if the mouse is being held down?

1 Like

@eizaray1234 Take a look at this page: ContextActionService | Documentation - Roblox Creator Hub

You will notice that there is a variable named InputState. When a mouse is down, it will be Enum.UserInputState.Begin, and when it ends, it will be Enum.UserInputState.End.

2 Likes

I’m sorry if my reply confused you. I will go a little more in depth.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local MouseHold = false

Mouse.Button1Down:Connect(function())
MouseHold = true
end)

Mouse.Button1Up:Connect(function())
MouseHold = false
end)

while true do
if MouseHold then
--Code
end
wait()
end

This script works like this; whenever the player holds down Left Click, it counts as Button1Down. This will then “enable” the set Variable which in this case is MouseHold. When the player then decides to stop holding the left mouse button, it will “Disable” / “Deactivate” the MouseHold variable. I have also tested this with an AutoClicker, and it is AutoClicker-Proof. I am one hundred percent certain that this works, because I have used this method myself several times already.

If you have any more questions, please don’t be afraid to send me a message!

I hope this helped!

Have a good day / night!

10 Likes

Another way you could detect when the player is holding down their mouse is using the UserInputService:IsMouseButtonPressed function (in a while loop presumably)

1 Like

You could possibly use the code below, its a check and better than a loop. If the player pressed a button and is holding it. Then it does what you do below, or if they don’t hold the button then you do what you want for it to do below.

UserInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 and Target then
– do stuff
end
end)

UserInputService.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 and Target then
– do stuff
end
end)

3 Likes