local function C(C,s)animation.AnimationId="\114\098\120\097\115\115\101\116\105\100\058\047\047"..s return C:LoadAnimation(animation)end local function s(C,s)s=s or 1 local p,A=C:ToEulerAnglesXYZ()return CFrame.new(C.Position)*CFrame.Angles(0,A*s,0)end local function p(C)if not npcModel:IsDescendantOf(workspace)then return end if not C then return end local p=C-npcModel.PrimaryPart.Position local A=rotationOffset*CFrame.new(Vector3.new(),p.Unit)or rotationOffset if not waveAnimationTrack.IsPlaying then NPC.Root.Transform=s(A,TwistProportions.Root)NPC.Waist.Transform=s(A,TwistProportions.Waist)end NPC.Neck.Transform=s(A,waveAnimationTrack.IsPlaying and TwistProportions.Full or TwistProportions.Neck)return p.Magnitude end
-- Helper functions
local function loadAnimation(controller, animationId)
	animation.AnimationId = "rbxassetid://" .. animationId
	return controller:LoadAnimation(animation)
end

local function isolateAndDampenYAngle(targetCFrame, proportionMultiplier)
	proportionMultiplier = proportionMultiplier or 1
	local _, y = targetCFrame:ToEulerAnglesXYZ()

	return CFrame.new(targetCFrame.Position) * CFrame.Angles(0, y * proportionMultiplier, 0)
end

local function makeNPCLookAt(lookAtPosition)
	-- If the streaming is enabled then NPC might be streamed out which in that case this function should return immediately.
	if not npcModel:IsDescendantOf(workspace) then
		return
	end

	if not lookAtPosition then
		return
	end
	local differenceVector = lookAtPosition - npcModel.PrimaryPart.Position
	local targetCFrame = rotationOffset * CFrame.new(Vector3.new(), differenceVector.Unit) or rotationOffset

	-- Let the waving animation move the body
	if not waveAnimationTrack.IsPlaying then
		NPC.Root.Transform = isolateAndDampenYAngle(targetCFrame, TwistProportions.Root)
		NPC.Waist.Transform = isolateAndDampenYAngle(targetCFrame, TwistProportions.Waist)
	end
	NPC.Neck.Transform = isolateAndDampenYAngle(
		targetCFrame,
		waveAnimationTrack.IsPlaying and TwistProportions.Full or TwistProportions.Neck
	)

	return differenceVector.Magnitude
end
