Attempting to make a player face towards a part

I’m trying to make a player face an NPC via CFrame,
however, when executing the code, the player model can’t move or walk around. In addition, I don’t want the player model to look up, or turn, only for them to turn left and right.
I’ve tried using FromMatrix, but it didn’t do anything.

script.Parent.OnServerEvent:Connect(function(plr,obj)
	local chr=plr.Character
	local rs=game:GetService("RunService")

	repeat   rs.Heartbeat:Wait() chr:SetPrimaryPartCFrame(CFrame.new(chr.PrimaryPart.Position, obj.Position)) until script.Parent.Parent.Tracking.Value==false
end)
--obj is the mouse.Target, which is set when the remote event is fired. Tracking is a bool value that says if the track command has been disabled.

I’m not knowledgeable on CFrames so I couldn’t tell ya how to fix it, but from understanding RunService I can see that the issue here is likely that, since you’re using RunService.Heartbeat, the player’s position is moved to chr.PrimaryPart.Position every single frame of the game, which means it will be impossible for the user to ever change location because ever frame of the game sets its location to one spot. See if there’s some sort of way to only change the lookvector of chr but not position

1 Like

Would a more ideal solution be using BodyGyro and setting the CFrame property?

2 Likes

You should probably use BodyGyro for this, while yes, you can manipulate the C0 on the Root Joint to create the same effect, BodyGyro simplifies the math and overall makes it a lot easier

Create a BodyGyro and set it’s torque to 0, 10000, 0 (a human rig may need more than 10,000 idk)
Then, you can just set the CFrame of the BodyGyro to your target part, (world space)

6 Likes

I don’t know a whole lot about BodyGyro, or how to use it to face a certain object.

You need ot use CFrame.FromMatrix() as CFrame.new(pos, lookat) is deprecated.

Thanks a bunch, this did the trick!

Update: This did work, and I got some info on how to do it, thank you!