local l=game:GetService("\082\117\110\083\101\114\118\105\099\101")local O local h={};(script.Parent:WaitForChild("\082\101\109\111\116\101\069\118\101\110\116")).OnServerEvent:Connect(function(l,O)h[l]=O end);(script.Parent:WaitForChild("\083\101\116\085\112")).OnServerEvent:Connect(function(A,K,Q)local f=A.Character or A.CharacterAdded:Wait()local C=f:WaitForChild("\072\101\097\100")local V=f:WaitForChild("\072\117\109\097\110\111\105\100")local H=f:WaitForChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")local j=f:WaitForChild("\084\111\114\115\111")local e=j:WaitForChild("\078\101\099\107")local S=H:WaitForChild("\082\111\111\116\074\111\105\110\116")local U=j:WaitForChild("\082\105\103\104\116 \072\105\112")local D=j:WaitForChild("\076\101\102\116 \072\105\112")local W=D.C0 local E=U.C0 local u=e.C0 local k=S.C0 l.Heartbeat:Connect(function(l)O=h[A]or Vector3.new(0,0,0)local V=K if f:FindFirstChild("\084\111\114\115\111")and f:FindFirstChild("\072\101\097\100")then local l=j.CFrame.lookVector local h=C.CFrame.p if e and S then if Q:IsDescendantOf(f)or Q:IsDescendantOf(A)and H.Anchored~=true then local A=(C.CFrame.p-O).magnitude local K=C.CFrame.Y-O.Y local Q=CFrame.Angles(-(math.atan(K/A)*.5),((h-O).Unit:Cross(l)).Y*1,0)e.C0=(e.C0:lerp(Q*u,.25)).Rotation+u.Position local f=-(math.atan(K/A)*.5)local V=((h-O).Unit:Cross(l)).Y*.5 local H=CFrame.Angles(f,V,0)local j=H*k S.C0=(S.C0:lerp(j,.25)).Rotation+k.Position local p=S.C0*k:Inverse()local w=p:Inverse()U.C0=w*E D.C0=w*W end end end end)end)
-- Game services
local RunService = game:GetService("RunService")
local Point
local playerMousePositionDictionary = {}

script.Parent:WaitForChild("RemoteEvent").OnServerEvent:Connect(function(playerThatFired, mousePosition)
	playerMousePositionDictionary[playerThatFired] = mousePosition
end)

script.Parent:WaitForChild("SetUp").OnServerEvent:Connect(function(Player, CamCFrame, CamSubject)
	local Character = Player.Character or Player.CharacterAdded:Wait()
	--task.wait(2.69)
	--Old R15 code
	local Head = Character:WaitForChild("Head")
	--local Neck = Head:WaitForChild("Neck")

	--local Torso = Character:WaitForChild("UpperTorso")
	--local Waist = Torso:WaitForChild("Waist")

	--New R6 stuff
	local Humanoid = Character:WaitForChild("Humanoid")
	local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

	local Torso = Character:WaitForChild("Torso")
	local Neck = Torso:WaitForChild("Neck")

	local Waist = HumanoidRootPart:WaitForChild("RootJoint")

	local RHip = Torso:WaitForChild("Right Hip")
	local LHip = Torso:WaitForChild("Left Hip")

	local LHipOriginC0 = LHip.C0
	local RHipOriginC0 = RHip.C0

	local NeckOriginC0 = Neck.C0
	local WaistOriginC0 = Waist.C0

	RunService.Heartbeat:Connect(function(dt)
		Point = playerMousePositionDictionary[Player] or Vector3.new(0, 0, 0)

		local CameraCFrame = CamCFrame

		if Character:FindFirstChild("Torso") and Character:FindFirstChild("Head") then
			local TorsoLookVector = Torso.CFrame.lookVector
			local HeadPosition = Head.CFrame.p

			if Neck and Waist then
				if
					CamSubject:IsDescendantOf(Character)
					or CamSubject:IsDescendantOf(Player) and HumanoidRootPart.Anchored ~= true
				then
					local Distance = (Head.CFrame.p - Point).magnitude

					local Difference = Head.CFrame.Y - Point.Y

					local goalNeckCFrame = CFrame.Angles(
						-(math.atan(Difference / Distance) * 0.5),
						(((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 1,
						0
					)
					Neck.C0 = Neck.C0:lerp(goalNeckCFrame * NeckOriginC0, 0.5 / 2).Rotation + NeckOriginC0.Position

					local xAxisWaistRotation = -(math.atan(Difference / Distance) * 0.5) -- Change 0.5 to change amount of rotation
					local yAxisWaistRotation = (((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 0.5
					local rotationWaistCFrame = CFrame.Angles(xAxisWaistRotation, yAxisWaistRotation, 0)
					local goalWaistCFrame = rotationWaistCFrame * WaistOriginC0
					Waist.C0 = Waist.C0:lerp(goalWaistCFrame, 0.5 / 2).Rotation + WaistOriginC0.Position

					local currentLegCounterCFrame = Waist.C0 * WaistOriginC0:Inverse()

					local legsCounterCFrame = currentLegCounterCFrame:Inverse()

					RHip.C0 = legsCounterCFrame * RHipOriginC0
					LHip.C0 = legsCounterCFrame * LHipOriginC0
				end
			end
		end
	end)
end)
