How to detect if mouse is dragged with left mouse button and by how much?

I want to create a 3D map of sorts. When a player drags their mouse with the left button, I want to map to change position/get dragged. Preferrably, I don’t want this to cause issues with also wanting a left click to perform an action when certain areas of the map are clicked.

I was going to investigate userinputservice methos but found something with ContextActivationService which almost works:

local UIS = game:GetService("UserInputService")
local CAS = game:GetService("ContextActionService")

local part = Instance.new("Part")
part.Anchored = true
part.Parent = workspace
part.Position = workspace.Starmap.orbitCenter.Position

script.Parent.Activated:Connect(function()
	CAS:BindAction("MouseMovement",function(name,state,input)
		local delta = input.Delta
		
		if delta == Vector3.new() then return end
		print(delta)
		part.CFrame = part.CFrame + Vector3.new(delta.X,0,delta.Y)
		
	end,false,Enum.UserInputType.MouseMovement)
end)

However this only works for the right mouse button. I don’t see what variable would determine weather the left or right button gets used.

It also doesn’t work when a certain gui is visible, even though another gui is always visible, and even on areas where there are no guis at all. Mabey it’s because the camera is set to scriptable when it’s visible? But I need it to be set to this.

I’d look through the api but since ive never used Context there’d be lots to read and it could be futile so hopefully you guys can help. If you know how i’d unbind it later, i’d like to know too.

2 Likes
local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()

local camera = workspace.CurrentCamera

mouse.Button1Down:Connect(function()
	local leftXStart, leftYStart = mouse.X, mouse.Y
	local leftMouseStart = tick()
	local connection do
		connection = mouse.Button1Up:Connect(function()
			local leftXEnd, leftYEnd = mouse.X, mouse.Y
			local leftMouseEnd = tick()
			print("Mouse moved "..math.abs(leftXEnd - leftXStart).." pixels across the X dimension and "..math.abs(leftYEnd - leftYStart).." pixels across the Y dimension!")
			print("Left mouse button held for "..(math.round(((leftMouseEnd - leftMouseStart) * 100)) / 100).." seconds!")
			connection:Disconnect()
		end)
	end
end)

mouse.Button2Down:Connect(function()
	local rightXStart, rightYStart = mouse.X, mouse.Y
	local rightMouseStart = tick()
	local connection do
		connection = mouse.Button2Up:Connect(function()
			local rightXEnd, rightYEnd = mouse.X, mouse.Y
			local rightMouseEnd = tick()
			print("Mouse moved "..math.abs(rightXEnd - rightXStart).." pixels across the X dimension and "..math.abs(rightYEnd - rightYStart).." pixels across the Y dimension!")
			print("Right mouse button held for "..(math.round(((rightMouseEnd - rightMouseStart) * 100)) / 100).." seconds!")
			connection:Disconnect()
		end)
	end
end)

image

1 Like
local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()

local camera = workspace.CurrentCamera

local function mouseButtonDown(buttonStart)
	local connectionLeft, connectionRight
	local xStart, yStart = mouse.X, mouse.Y
	local mouseStart = tick()

	local function mouseButtonUp(buttonEnd)
		if buttonStart == buttonEnd then
			local xEnd, yEnd = mouse.X, mouse.Y
			local mouseEnd = tick()
			print("Mouse moved "..math.abs(xEnd - xStart).." pixels across the X dimension and "..math.abs(yEnd - yStart).." pixels across the Y dimension!")
			print("Mouse button held for "..(math.round(((mouseEnd - mouseStart) * 100)) / 100).." seconds!")
			if buttonEnd == "Left" then
				connectionLeft:Disconnect()
			elseif buttonEnd == "Right" then
				connectionRight:Disconnect()
			end
		end
	end
	
	if buttonStart == "Left" then
		connectionLeft = mouse.Button1Up:Connect(function()
			mouseButtonUp("Left")
		end)
	elseif buttonStart == "Right" then
		connectionRight = mouse.Button2Up:Connect(function()
			mouseButtonUp("Right")
		end)
	end
end

mouse.Button1Down:Connect(function()
	mouseButtonDown("Left")
end)

mouse.Button2Down:Connect(function()
	mouseButtonDown("Right")
end)

and here’s a merger of the two functions into one.

2 Likes