How do i rotate the part to follow the player

i followed a tutorial how to make a draggable part but how do i rotate it to face towards the player because right now i can move it but it doesnt rotate its also not setting anchored to false when you drop the part

-- Services
local uis = game:GetService("UserInputService")
local workspace = game:GetService("Workspace")

-- Variables
local picked_up_part = false
local part = nil

-- Input handler
uis.InputBegan:Connect(function(input)
	local ray = workspace.CurrentCamera:ViewportPointToRay(uis:GetMouseLocation().X, uis:GetMouseLocation().Y)

	if input.UserInputType == Enum.UserInputType.MouseButton1 and not picked_up_part then
		for i, v in pairs(workspace:GetDescendants()) do
			if v:HasTag("Item") and (ray.Origin + ray.Direction * (ray.Origin - v.Position).Magnitude - v.Position).Magnitude <= 1 then
				picked_up_part = true
				part = v
				local connection = nil
				connection = game:GetService("RunService").RenderStepped:Connect(function()
					if not picked_up_part then connection:Disconnect() return end
					local ray = workspace.CurrentCamera:ViewportPointToRay(uis:GetMouseLocation().X, uis:GetMouseLocation().Y)
					part.Position = ray.Origin + ray.Direction * 10
					part.Orientation = Vector3.new(0, 0, 0)
					part.CanCollide = false
					part.Anchored = false
				end)
			end
		end
	elseif input.UserInputType == Enum.UserInputType.MouseButton2 and picked_up_part then
		-- Release the picked up part
		picked_up_part = false
		part.Anchored = true
		part.CanCollide = true
		part = nil
	end
end)
1 Like

Instead of updating part.Position and part.Orientation separately, you can do:

local characterPosition = character.PrimaryPart.Position
part.CFrame = CFrame.lookAt(ray.Origin + ray.Direction * 10, characterPosition)

if you only want it to rotate in the XOZ plane, make the Y component equal.

local XOZ_PLANE = Vector3.new(1, 0, 1)
local partPosition = ray.Origin + ray.Direction * 10
local characterPosition = character.PrimaryPart.Position * XOZ_PLANE + Vector3.yAxis  * partPosition.Y -- Equivalent to Vector3.new(characterPosition.X, partPosition.Y, characterPosition.Z) but different practices i guess
part.CFrame = CFrame.lookAt(partPosition, characterPosition)
1 Like

thanks! the first one works perfectly

1 Like

@Imagine_Developing i figured out that its not anchoring but it stays in the air is what you wrote supposed to fix this

1 Like