How to rotate a part based on the cameras rotation?

I’m trying to rotate a part to always look where the player is looking (I have a custom character and the game is in first person so basically this will be the characters head)
The rotation detecting worked fine, and when on its own as just the part it all seemed to work, but when added to my StarterCharacter the world starts shaking up and down, the character cant move, and the rotation becomes laggy.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

ReplicatedStorage.Events.RotateHead.OnServerEvent:Connect(function(plr, rotation, pos)
	local char = plr.Character or plr.CharacterAdded:Wait()
	char.HumanoidRootPart.Rotation = rotation
end)

And here’s the rotation detection, which works fine

local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local cam = workspace.CurrentCamera

function CFrameToOrientation(cf)
	local x, y, z = cf:ToOrientation()
	return Vector3.new(math.deg(z*-1), math.deg(y + 300), math.deg(x*-1))
end

cam:GetPropertyChangedSignal("CFrame"):Connect(function()
	ReplicatedStorage.Events.Rotate:FireServer(CFrameToOrientation(cam.CFrame, script.Parent.HumanoidRootPart.Position))
end)

Is this even the best way to go about this? Again, the “head” is gonna be on a startercharacter and is just one part. Basically so other players can see where you are looking

1 Like

I’m not sure how well it’d work, but I’d try:

  • disabling the property in the humanoid that kills humanoids when the head isn’t connected to anything
  • making the head disconnected completely from the body, anchored on clientside, unanchored on serverside
    • the client should have network ownership over the head part because it’s part of the starterCharacter, but if not, have the server give network ownership to that player
  • using a local script to set the head’s cframe to the appropriate cframe inside of a function connected to RunService.Heartbeat
    • network ownership should handle replicating this to serverside