Part Grab Dosent work Help Fix it pls

Here The Code :

local player = game:GetService(“Players”).LocalPlayer

local mouse = player:GetMouse()

local blue = game.Workspace.Folder:GetChildren()

for i, v in pairs(blue) do
mouse.Button1Down:Connect(function()
if mouse.Target == game.Workspace.Folder:FindFirstChild(v.Name) then

v.Position = Vector3.new(mouse.Target)

end
end)
end

There’s a mistake in your code: v.Position=Vector3.new(mouse.Target.Position)
Though I don’t think this will properly help you create a part dragger.

how i need fix it?? pls help me solve it

There are multiple issues with your code.

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.

soo what i need do this??

local player = game:GetService(“Players”).LocalPlayer

local mouse = player:GetMouse()

local blue = game.Workspace.Folder:GetChildren()

for i, v in pairs(blue) do
mouse.Button1Down:Connect(function()
if mouse.Target == game.Workspace.Folder:FindFirstChild(v.Name) then

v.Position = Vector3.new(mouse.Hit.X)

end
end)
end

Secend i need first click it and then move

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.

Isn’t Mouse.Hit.p deprecated?
You had a good script edit tho, just maybe use newer methods :slight_smile:

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.

2 Likes