R15 UpperTorso following Camera

Hello! I do tools for my game and I need the upper part of the body (UpperTorso) to follow the camera. I made a script and does work but It doesn’t have a limit. You can rotate your body about 360°. I did look for solutions on Developer Hub, but I didn’t find anything.

Local Script:

local player = game:GetService("Players").LocalPlayer
local Camera = game.Workspace.CurrentCamera
local character = player.Character or player.CharacterAdded:Wait()

-- LowerTorso = UpperTorso
-- LowerTorso.Root = UpperTorso.Waist
-- I changed it from LowerTorso to UpperTorso because I thought that LowerTorso rotates the upper part of the body.

local lowerTorso = character:FindFirstChild("UpperTorso") or character:WaitForChild("UpperTorso")
local root = lowerTorso:FindFirstChild("Waist") or lowerTorso:WaitForChild("Waist")

local RC = CFrame.lookAt(root.C0.Position, root.C0.Position + Camera.CFrame.LookVector) -- RC means Waist.C0
local LTCF = CFrame.new(lowerTorso.Position) * root.C0.Rotation -- LTCF means UpperTorso.CFrame

game:GetService("RunService").RenderStepped:Connect(function()
		RC = CFrame.lookAt(root.C0.Position, root.C0.Position + Camera.CFrame.LookVector)
		LTCF = CFrame.new(lowerTorso.Position) * root.C0.Rotation

		script.Parent.Event:FireServer(RC, LTCF)
end)

Server Script:

local event = script.Event
local char = script.Parent
local uppertorso = char:WaitForChild("UpperTorso")
local waist = uppertorso:WaitForChild("Waist")

event.OnServerEvent:Connect(function(player, RC, LTCF) -- player, Waist.C0 to set, UpperTorso.CFrame to set.
	if player.Character == char then
		waist.C0 = RC
		uppertorso.CFrame = LTCF
	end
end)
local Game = game
local Workspace = workspace
local RunService = Game:GetService("RunService")
local Players = Game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local UpperTorso = Character:FindFirstChild("UpperTorso") or Character:WaitForChild("UpperTorso")
local Waist = UpperTorso:FindFirstChild("Waist") or UpperTorso:WaitForChild("Waist")
local Camera = Workspace.CurrentCamera

local function Clamp(Value)
	return math.clamp(Value, -math.pi / 4, math.pi / 4) --Increase/decrease '4' for decreased/increased range of motion respectively.
end

local function OnRenderStep()
	local X, Y, Z = Camera.CFrame:ToOrientation(Camera.CFrame)
	Waist.C0  = CFrame.new(Waist.C0.Position) * CFrame.fromOrientation(Clamp(X), Clamp(Y), Clamp(Z))
end

RunService.RenderStepped:Connect(OnRenderStep)
1 Like