The first issue is that it only updates the position of the part when the Button1Down event is fired. Instead, you need to update the part position when the mouse is moved.
Second, you’re attempting to set the part position to the Target property which is only the object the mouse is pointing towards. Instead, you need to use the Hit property which is the CFrame of the mouse’s position in 3D space.
Below is a working example of a part grabber with comments explaning how it works.
local Player = game:GetService('Players').LocalPlayer
local Mouse = Player:GetMouse()
local SelectedPart
Mouse.Button1Up:Connect(function()
SelectedPart = nil -- Unselect part when the player releases their mouse
end)
Mouse.Button1Down:Connect(function()
local target = Mouse.Target
if not target then return end -- Check if there is a target
if not target:IsDescendantOf(workspace.GrabbableParts) then return end -- Check if target is a descendant of the folder
SelectedPart = target
end)
Mouse.Move:Connect(function()
local MousePositionIn3DSpace = Mouse.Hit.Position -- A vector3 containing the coordinates of the mouse's position in 3D space (aka Workspace)
if not SelectedPart then return end -- Check if the player is currently grabbing a part
SelectedPart.Position = Vector3.new(MousePositionIn3DSpace.X, .5, MousePositionIn3DSpace.Z)
end)
Sidenote: You should edit your responses rather than replying multiple times.
Nope! Please make sure to check if something is deprecated by using the developer hub. If something is deprecated, then you will see this at the top of the documentation page:
The hit property of mouse is most definitely not deprecated. As far as I know, it is also the only API built into the engine to get the position of the mouse in 3D space.
I actually have to disagree there: @iGottic is correct. The mention was also of your use of Mouse.Hit.p rather than of Mouse.Hit itself.
Mouse.Hit is a CFrame and CFrame.p is deprecated in favour of CFrame.Position. You should be using the latter as it’s better for the sake of readability and knowing what it is that you’re indexing.
Most Roblox properties and methods in lowercase were deprecated in favour of PascalCase. In this case, a single letter property in lowercase was deprecated for a more clear PascalCase property.
Datatypes don’t display the big header deprecated header because that’d mean the whole CFrame datatype is deprecated. Deprecated items in datatypes aren’t shown or are crossed out.
Mouse.Hit is internally a raycast too, thought you should know. It’s certainly not the only way to get the mouse’s position in 3D space, it just condenses the work for you.