LookAt Mouse.Hit seems to not be working with meshParts

So the CFrame.lookAt() thing seems to not work with meshParts but it does with normal Parts, so:

This works:

local mouse = game:GetService("Players").LocalPlayer:GetMouse()
local part = workspace.Part --This is a Part


while task.wait() do
	CFrame.lookAt(part.Position, mouse.Hit.Position)
end

But this doesnt:

local mouse = game:GetService("Players").LocalPlayer:GetMouse()
local part = workspace.MeshPart --This is a meshPart


while task.wait() do
	CFrame.lookAt(part.Position, mouse.Hit.Position)
end

Please tell me if this is normal or not, and if it is how can i work around it?

1 Like

If the Mouse.Hit isn’t working as expected with MeshParts in Roblox, you might need to ensure that the MeshPart has a proper collision set up and that your script is correctly handling the Mouse object. Here’s a basic script to help you get started with making an object look at where the mouse is hitting, including handling MeshParts.


local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local objectToRotate = workspace.Object -- Replace with your object

mouse.Move:Connect(function()
    local targetPosition = mouse.Hit.p
    local objectPosition = objectToRotate.Position
    local direction = (targetPosition - objectPosition).Unit
    local newCFrame = CFrame.lookAt(objectPosition, targetPosition)

    objectToRotate.CFrame = newCFrame
end)

Make sure to:

  1. Set the Object: Replace workspace.Object with the path to the object you want to rotate.
  2. Collision Settings: Ensure that the MeshPart or any other part has proper collision settings enabled so that the Mouse.Hit can register hits on it.
  3. Position Handling: The script assumes the object is not anchored. If it is anchored, you might need additional logic to rotate it appropriately.

Troubleshooting Tips:

  1. Check Collision: Verify that the MeshPart has its CanCollide property set correctly to allow the mouse to register hits.
  2. Print Statements: Use print statements to debug and check values of mouse.Hit and objectToRotate.Positionto ensure they are what you expect.
  3. Parent-Child Relations: Ensure that the object you are trying to rotate is properly parented in the workspace.

If the script still does not work as expected, let me know the specific behavior you’re observing, and I can help you further troubleshoot the issue.

1 Like

Thanks for your help! It works now

1 Like

Oh yeah and for some reason it doesnt work on character parts, could this be because of the Motor6D’s?

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.