Rotating a part to face character?

So I am working on an object placement system. Right now it works alright and I can get it to move the part to where the mouse is but if I add the line that tries to rotate the part I get an error. So here is the relevant code:

local hover = mouse.Hit
local x, y ,z = CFrame.fromEulerAnglesXYZ(chr.Head.Position.x, chr.Head.Position.y, chr.Head.Position.z)
wait()
mc:SetPrimaryPartCFrame(CFrame.new(hover.x, hover.y, hover.z))
mc:SetPrimaryPartCFrame(CFrame.Angles(x, y ,z))

Chr is a variable that is set to the character. Then right here is the error that comes up:

[Players.CrispyBrix.PlayerGui.Menus.BuildBox.WoodWall.CraftBut.LocalScript:25: bad argument #3 to ‘Angles’ (number expected, got nil)

This is in a localscript in a GUI in the player. Its local because I don’t want others seeing what is being placed until it is placed. Basically my question is why is the characters body positions returning nil? Is it a problem with my code or would it be something unrelated like I have the variable set to the wrong place?

2 Likes
local Firstpos = Vector3.new(hover.x, hover.y, hover.z)
local PositionYouWantToLookAt = root.CFrame.p  --I've honestly never noticed a difference, between the CFrame and Position in this type of use-case 

mc:SetPrimaryPartCFrame(CFrame.new(Firstpos, PositionYouWantToLookAt))

https://developer.roblox.com/api-reference/datatype/CFrame

“Creates a CFrame located at pos with it’s lookVector pointing towards the lookAt position.”

3 Likes
  1. fromEulerAnglesXYZ returns a cframe
    you would have to get the difference between the chr position and the hover position, put it into fromEulerAngles, bam, you have your angles now

  2. alternatively, Cframe.new(Vector3, Vector3) will allow you to get a cframe positioned at vector3 facing the Vector3, just call toEulerAnglesXYZ from that and turn that into angle, but this depends on what mc is, it is another part? its only going to rotate relative to where the head is compared to the mouse hit

1 Like

I have tried this. It gives me:

[Invalid number of arguments: 4]

“mc” is a copied model.

local mf = game.ReplicatedStorage.BuildLights:FindFirstChild(item.Value)
local mc = mf:Clone()

There was a slight bug that didn’t convert the coordinates into a Vector 3 in @Lua_Basics’s code, this fixed version should work for you:

local LookAt = Vector3.new(0, 0, 0)
mc:SetPrimaryPartCFrame(CFrame.new(Vector3.new(hover.x, hover.y, hover.z), LookAt))

And you’d set the LookAt to any position you want it to look at, for example:

local LookAt = Target.CFrame.Position.

1 Like

Ahh yes, I forgot the Vector3 was more of a fast post for me.

1 Like