Mouse being holded

I am creating a plugin, and I need to detect when the user which is using the plugin, has their mouse holded.

If they are holding the left mouse button, I need it to print the position they are holding it at. If they are holding the mouse button and their mouse moves I need to print the new position. So something like this:

if holded then
 print(mouse.Hit)

However I need to print “cancelled!” if they released the left mouse button.

Any help is appreciated!

Could you not use these events that is part of the mouse?

Yes you can, also I have figured out the question that I asked but I have another question as well.

Here is my code:

local holding = false
local mouseMovingDebounce = false

local function mouseMoving()
	local position = mouse.Hit.p
	local target = mouse.Target
	
	print(position)
end

local function mouseHolding()
	holding = true
	print("Left mouse button IS BEING held!")
	
	mouse.Move:Connect(function()
		if mouseMovingDebounce == false then
			mouseMovingDebounce = true
			mouseMoving()
		else
			task.wait(1)
			mouseMovingDebounce = false
		end
	end)
end

local function mouseReleased()
	if not holding then return end
	
	holding = false
	print("Left mouse button is NO LONGER being held!")
end

mouse.Button1Down:Connect(mouseHolding)
mouse.Button1Up:Connect(mouseReleased)

I am trying to add a debounce so that when the mouse moves it doesn’t go crazy and print the position a 100 times in a second. The method for the debounce I am currently using isn’t working do you know how I would add a debounce in this code?

Maybe something like this?

local holding = false
local mouseMovingDebounce = false

local function mouseMoving()
	local position = mouse.Hit.p
	local target = mouse.Target
	
    If mouseMovingDebounce == false then
	     print(position)
          mouseMovingDebounce = true
          task.wait(1)
          mouseMovingDebounce = false
    end
end

local function mouseHolding()
	holding = true
	print("Left mouse button IS BEING held!")
	
    mouse.Move:Connect(function()
		mouseMoving()
	end)
end

local function mouseReleased()
	if not holding then return end
	
	holding = false
	print("Left mouse button is NO LONGER being held!")
end

mouse.Button1Down:Connect(mouseHolding)
mouse.Button1Up:Connect(mouseReleased)
1 Like

Wow that works thanks for the help!