Hey everyone,

I've been trying to use a click to move object script from YouTube and I've wanted to do 2 things. Note that this is in First Person Camera:

  1. Keeping objects a certain distance away from the player when holding the object

  2. Only be able to move specific objects, excluding the rest in the game.

I'll put my code below located in StarterPlayer>StarterPlayerScripts. I'd appreciate it if someone could let me know what to modify, or, even better, rewrite it so it fits the criteria.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mtarget
local down

function clickObj()
	if mouse.Target ~= nil and mouse.Target.Name ~= game.Workspace.Baseplate then
		mtarget = mouse.Target
		print(mtarget)
		mouse.TargetFilter = mtarget
		print(mouse.TargetFilter)
		down = true
	end
end
mouse.Button1Down:connect(clickObj)

function moveMouse()
	if down and mtarget then
		local posX,posY,posZ = mouse.Hit.X,mouse.Hit.Y,mouse.Hit.Z
		
		mtarget.Position = Vector3.new(posX,posY,posZ)
	end
end
mouse.Move:connect(moveMouse)

function mouseDown()
	down = false
	mouse.Target = nil
	mouse.TargetFilter = nil
end
mouse.Button1Up:connect(mouseDown)

5 comments

0

use code block plssss

0

No worries, sorted.

0
0

I'll take a look

0

use the targetfilter property of the mouse