So im making this tool that when complete will allow you to rope to where you click but my problem is that I cant get it to where i click. If i click it'll only work on the baseplate but on other objects it'll be in the wrong position. I think its something to do with the mouse not stopping the path it'll stop just before going over the object but it'll be going in a straight line. So I might need a script to prevent that from happening. The part of the script that im having problems is

if mouse.Hit.p then			
	a1.Position = mouse.Hit.p
end

The rest all works fine.

script.Parent.Parent.Equipped:connect(function(mouse)
	mouse.Button1Down:connect(function()
		local target = mouse.Target
		if target ~= nil then
			local humanoid = script.Parent.Parent.Parent:FindFirstChild("Humanoid")
			local a0 = Instance.new("Attachment")
			a0.Parent = humanoid.Parent.Torso --where to attach on the body
			local a1 = Instance.new("Attachment")
			a1.Parent = target
			if mouse.Hit.p then         
   				a1.Position = mouse.Hit.p
			end
			local rc = Instance.new("RopeConstraint")
			rc.Parent = humanoid.Parent.Torso
			rc.Attachment0 = a0
			rc.Attachment1 = a1
			rc.Visible = true 
			if script.Parent.Parent.AutoLength.Value == true then
				rc.Length = (humanoid.Parent.Torso.Position - target.Position).magnitude
			else 
				rc.Length = 25
			end
			rc.Thickness = 0.7	
			rc.Color = BrickColor.new("Lily white")
			local gyro = Instance.new("BodyGyro")
			gyro.Parent = humanoid.Parent.Torso
			if target.Parent:FindFirstChild("Humanoid") then
				local gyrotarget = Instance.new("BodyGyro")
				gyrotarget.Parent = target.Parent.Torso
			end
		end
	end)
end)

The game if you need it, use the roper tool. https://www.roblox.com/games/724292260/Test