Upon clicking on a Model, the model gets tied to the mouse even though M1 isn't down

  1. What do you want to achieve? Keep it simple and clear!
    So I’m trying to make it, so whenever player clicks on a part and then clicks on a button, that appears on the screen. It lets them drag the part when they hold their mouse on it. When they are dragging anywhere BUT the part, the drag the cam instead.

  2. What is the issue? Include screenshots / videos if possible!
    Draging parts itself is fine, but when it comes to dragging Models I get this problem. Upon clicking a model, it gets tied to the mouse position even thought M1 isn’t down.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried looking into dev hub. I tried looking around devforum. I tried googling for like 2 hours straight as well editing my code a lot.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local scrollAmount = 5
local fovTarget = 70
local maxZoom = 100
local smallestZoom = 20
local currentCamPos = 0
UserInputService.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseWheel then
		local up = input.Position.Z > 0 and -scrollAmount or scrollAmount

		if (currentCamPos >= maxZoom) and up == scrollAmount then
			return
		elseif (currentCamPos <= smallestZoom) and up == -scrollAmount then
			return
		end

		fovTarget += up
	end
end)

RunService.Heartbeat:Connect(function()
	local camera = workspace:FindFirstChild("Camera")
	local fovCamera = camera.FieldOfView

	if fovTarget > maxZoom then
		fovTarget = maxZoom
	elseif fovTarget < smallestZoom then
		fovTarget = smallestZoom
	end

	local fovDifference = fovTarget - fovCamera

	camera.FieldOfView += fovDifference
	
	currentCamPos = camera.FieldOfView
end)

game:GetService("ReplicatedStorage").Events.Dragging.Event:Connect(function(building: instance, bool: boolean)
	draggingBuilding = bool
	selectedBuilding = building
end)

local target = nil
local down = false
local dragCameraInstead = true
local MoveBuilding = require(game:GetService("ReplicatedStorage").ClientModules.MoveBuilding)

UserInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if mouse.Target ~= nil and mouse.Target.Locked == false and draggingBuilding then
			target = mouse.Target
			if target:FindFirstAncestorOfClass("Model") then
				target = MoveBuilding:GetParent(target, "Model")
			end
			mouse.TargetFilter = target
			print(mouse.TargetFilter)
			down = true
		end
	end
	print("MOUSE DOWN")
	print(down, mouse.TargetFilter, target)
end)

UserInputService.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		down = false
		mouse.TargetFilter = nil 
		target = nil
	end
	
	print("MOUSE UP")
	print(down, mouse.TargetFilter, target)
end)

local function RoundVector(value: Vector3, grid: number)
	return value - Vector3.new(value.X % grid, 0, value.Z % grid)
end

mouse.Move:Connect(function()
	if down and target and target == selectedBuilding then
		dragCameraInstead = false
		
		--print(down, target, selectedBuilding)
		
		--print(target.ClassName)
		
		if target:isA("Model") then
			target:MoveTo(RoundVector(Vector3.new(mouse.Hit.Position.X, 0.5, mouse.Hit.Position.Z), 1))
			print(target)
		else
			target.Position = RoundVector(Vector3.new(mouse.Hit.Position.X, 0.5, mouse.Hit.Position.Z), 1)
		end
		
	else
		dragCameraInstead = true
	end
end)

Put print(“MOUSE UP”) and print(“MOUSE DOWN”) into the if statements and you will see that Began runs before Ended

Oh yea… I’m dumb. Well in that case I have no Idea what could cause this problem.

It’s because multiple inputs could be ending before MouseButton1 begins.

It’s weird. When it comes to dragging a part itself, it’s fine. But when it comes to dragging a model, it just breaks…

Would sending the whole script help maybe?

Feels like Mouse1 ended runs before Mouse1 begin can finish running…
image