Body follow the camera

Hello! I do tools for my game and I need to body (lower torso) face the same as the camera but only up and down. I made a script and it doesn’t work right. Sometimes body face on the other side. I did look for solutions on Developer Hub but I didn’t find what I want.

Local Script:

local Char = game:GetService("Players").LocalPlayer.Character
local Torso = Char:WaitForChild("Torso", 1)
local Head = Char:WaitForChild("Head")
local Root = Char:WaitForChild("HumanoidRootPart")
local Humanoid = Char:WaitForChild("Humanoid")
local Evt = script.Parent:WaitForChild("Evt")
local TorsoJoint = Char:WaitForChild("LowerTorso")

local CamCF = CFrame.new()
local OriginalTorsoCF = Char:WaitForChild("LowerTorso").Root.C1
local CurrentZRot = 0

local ANGLE_LIMIT_X = {-65, 50} --Down, Up
local ANGLE_LIMIT_Y = {0, 0} --Left, Right

game:GetService("RunService").RenderStepped:Connect(function ()
	if Humanoid.Health <= 0 then
		return
	end
	local RootRX, RootRY, RootRZ = Root.CFrame:toOrientation()
	local RootRotation = CFrame.fromOrientation(-RootRX, RootRY, RootRZ)
	local CamRotX, CamRotY, CamRotZ = workspace.CurrentCamera.CFrame:toOrientation()
	local CamRot = CFrame.fromOrientation(
		CamRotX * -1, 
		CamRotY, 
		CamRotZ
	)
	local Rotation = RootRotation:toObjectSpace(CamRot)
	local RX, RY2, RZ = Rotation:toOrientation()
	local RX = math.rad(math.clamp(math.deg(RX), unpack(ANGLE_LIMIT_X)))
	local RY = math.rad(math.clamp(math.deg(RY2), unpack(ANGLE_LIMIT_Y)))
	Rotation = CFrame.fromOrientation(RX * -1, RY * -1, RZ * -1)
	Evt:FireServer(OriginalTorsoCF * Rotation)
end)

Server Script:

local Evt = script:WaitForChild("Evt")
local Char = script.Parent

Evt.OnServerEvent:Connect(function (Client, CF)
	if Client.Character == Char then
		Char:WaitForChild("LowerTorso").Root.C1 = CF
	end
end)
local Game = game
local Workspace = workspace
local Camera = Workspace.CurrentCamera
local RunService = Game:GetService("RunService")
local Players = Game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local LowerTorso = Character:FindFirstChild("LowerTorso") or Character:WaitForChild("LowerTorso")
local Root = LowerTorso:FindFirstChild("Root") or LowerTorso:WaitForChild("Root")

local function OnRenderStepped()
	Root.C0 = CFrame.lookAt(Root.C0.Position, Root.C0.Position + Camera.CFrame.LookVector)
	LowerTorso.CFrame = CFrame.new(LowerTorso.Position) * Root.C0.Rotation
end

RunService.RenderStepped:Connect(OnRenderStepped)
2 Likes

There’s a premade module for this, here it is:

The page with information about it:
https://developer.roblox.com/en-us/resources/developer-modules/social-interactions