local function O(O,K)animation.AnimationId="\114\098\120\097\115\115\101\116\105\100\058\047\047"..K return O:LoadAnimation(animation)end local function K(O,K)K=K or 1 local a,R=O:ToEulerAnglesXYZ()return CFrame.new(O.Position)*CFrame.Angles(0,R*K,0)end local function a(O)if not npcModel:IsDescendantOf(workspace)then return end if not O then return end local a=O-npcModel.PrimaryPart.Position local R=rotationOffset*CFrame.new(Vector3.new(),a.Unit)or rotationOffset if not waveAnimationTrack.IsPlaying then NPC.Root.Transform=K(R,TwistProportions.Root)NPC.Waist.Transform=K(R,TwistProportions.Waist)end NPC.Neck.Transform=K(R,waveAnimationTrack.IsPlaying and TwistProportions.Full or TwistProportions.Neck)return a.Magnitude end

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

