Only allow mouse to select certain parts

Good day,

I am trying to write a system that allows players to pick up certain parts using the mouse and move them, the code below is what I have so far.

I am having issues finding a way of only allowing players to pick up certain parts, I have looked into the mouse filter but I don’t think it will work?

Does anyone know a good way of determining what the mouse.target has detected and only processes if a certain part(s)?

-- ContextActionService is a game service that allows a game to bind user input to contextual actions
local CAS = game:GetService("ContextActionService")

-- Globals
local GrabObject = nil
local GrabStart = false
local Dragger = nil

-- Player
local player = game.Players.LocalPlayer
local character = player.Character
local mouse = player:GetMouse()
mouse.TargetFilter = workspace.OtherParts
local camera = game.Workspace.CurrentCamera

-- Grab Block function
function Grab(actionName, UserInputState, InputObject)
	
	print("Grab Function ran")
	
	player.PlayerGui.ScreenGui.Frame.Part.Text = "Part = " .. tostring(mouse.Target)
	
	-- If action name is Grab
	if actionName == "Grab" then
		
		-- state of an input that is currently or was recently performed
		if UserInputState == Enum.UserInputState.Begin then
			
			-- Find the distance between mouse Hit and player Head
			local Magnitude = (mouse.Hit.Position - character.Head.CFrame.Position).magnitude
			
			-- If distance les then 15 then
			if Magnitude < 15 then
				
				-- If the mouse has a target
				if mouse.Target then

					GrabObject = mouse.Target
					GrabStart = true
					
					GrabObject.Transparency = .2
					-- BrickColor.new(255,0,0)
					GrabObject.Color = Color3.fromRGB(255, 0, 0)
					
					-- Create the dragball					
					local DragBall = createDragBall()
					DragBall.CFrame = mouse.Hit
					Dragger = DragBall
					
					-- Create Beam
					local beam = Instance.new("Beam", workspace)
					beam.Attachment0 = GrabObject.Attachment
					beam.Attachment1 = character.HumanoidRootPart.RootRigAttachment
					beam.TextureLength = 5
					beam.Width0 = 1
					beam.Width1 = 1
					beam.Texture = "rbxasset://textures/particles/sparkles_main.dds"

					mouse.TargetFilter = GrabObject
					
					-- Weld DragBall to Grabbed Object
					local DragBallWeld = weldBetween(DragBall,GrabObject)
					
					addMover(DragBall)
					
					-- While player is dragging
					while Dragger do
						
						--Create ray from the users head to the mouse
						local cf = CFrame.new(character.Head.Position, mouse.Hit.Position)
						mouse.Icon = "http://www.roblox.com/asset?id=7641026979"
						Dragger.Mover.Position = (cf + (cf.LookVector * 10)).Position
						Dragger.RotMover.CFrame = camera.CFrame * CFrame.Angles(Dragger.RotOffset.Value.X, Dragger.RotOffset.Value.Y, Dragger.RotOffset.Value.Z)
					
						wait()
						
					end
					mouse.TargetFilter = nil
					mouse.Icon = "http://www.roblox.com/asset?id=7641025737"
					beam:Destroy()
					
				end

			end
			
		-- Plyer stops pressing button
		elseif UserInputState == Enum.UserInputState.End then
			
			GrabObject.Color = Color3.fromRGB(126, 104, 63)
			GrabObject.Transparency = 0
			GrabObject = nil
			GrabStart = false
			
			if Dragger then
				Dragger:Destroy()
				Dragger = nil
			end -- dragger

		end
		
	end -- if grab
		
end


function weldBetween(a, b)
	local weld = Instance.new("ManualWeld", a)
	weld.C0 = a.CFrame:inverse() * b.CFrame
	weld.Part0 = a
	weld.Part1 = b
	return weld
end


function addMover(part)
	local newMover = Instance.new("BodyPosition")
	newMover.Parent = part
	newMover.MaxForce = Vector3.new(40000,40000,40000)
	newMover.P = 40000
	newMover.D = 1000
	newMover.Position = part.Position
	newMover.Name = "Mover"

	local newRot = Instance.new("BodyGyro")
	newRot.Parent = part
	newRot.MaxTorque = Vector3.new(3000,3000,3000)
	newRot.P = 3000
	newRot.D = 500
	newRot.CFrame = game.Workspace.CurrentCamera.CFrame
	newRot.Name = "RotMover"

	local RotOffset = Instance.new("CFrameValue")
	RotOffset.Name = "RotOffset"
	RotOffset.Parent = part
end


function createDragBall()
	local DragBall = Instance.new("Part")
	DragBall.BrickColor = BrickColor.new("Electric blue")
	DragBall.Material = Enum.Material.Wood
	DragBall.Size = Vector3.new(.2,.2,.2)
	DragBall.Shape = "Ball"
	DragBall.Name = "DragBall"
	DragBall.Parent = workspace
	return DragBall
end

-- BindAction will bind an action to user input given an action handling function
-- actionName
-- functionToBind
-- createTouchButton
-- priorityLevel
-- inputTypes
--              Name   Function CreateButton InputType
CAS:BindAction("Grab", Grab, false, Enum.UserInputType.MouseButton1)

I mean, it depends what you want to filter. If you don’t want them to touch humanoids, just do

if mouse.Target and not mouse.Target.Parent:FindFirstChild("Humanoid") then

else
   return
end

or if you want a value, you can add some sort of bool value called whatever, let’s say “Unmoveable”

if mouse.Target and not Mouse.Target:FindFirstChild("Unmoveable") then
   --moveable
else
   --unmoveable
end
1 Like