Animation Movement Direction Changes Based On HeadRotation?

As it says in the title, Somehow my local script that changes the walk animation depending on the direction the player is facing, somehow changes because the neck motor changes, and i don’t know why, is there any fix to this ?

When i disable the head rotation local script, this bug gets fixed, but now i can’t make it so the head faces the camera

Animation direction local script :

task.wait(.15)
local runService = game:GetService("RunService")
local character = script.Parent
local Humanoid = character:WaitForChild("Humanoid")

local UIS = game:GetService("UserInputService")
--script:WaitForChild("leftwalk"):WaitForChild("Animation")
local currently_playing_anim = {}
local anim = {
	["frontwalk"] = Humanoid:LoadAnimation(script.forwardwalk);
	["backwalk"] = Humanoid:LoadAnimation(script.backwalk);
	["rightwalk"] = Humanoid:LoadAnimation(script.rightwalk);
	["leftwalk"] = Humanoid:LoadAnimation(script.leftwalk);
	["frontrun"] = Humanoid:LoadAnimation(script.forwardrun)
}
local X,Z = 0,0

local function StopAllAnims()
	for i,v in pairs(anim) do
		v:Stop()
	end
end


runService.RenderStepped:Connect(function()
	local movedir = character.PrimaryPart.CFrame:vectorToObjectSpace(Humanoid.MoveDirection)
	local currentstate = Humanoid:GetState()
	X = movedir.X
	Z = movedir.Z
	local distZ,distZMin = ((character.PrimaryPart.CFrame.LookVector * 5) - Vector3.new(0,0,Z)).Magnitude,((character.PrimaryPart.CFrame.LookVector * -5) - Vector3.new(0,0,Z)).Magnitude
	--print("X = "..movedir.X.." Z = "..movedir.Z)
	if currentstate == Enum.HumanoidStateType.Jumping then
		StopAllAnims()
	elseif currentstate == Enum.HumanoidStateType.Freefall then
		StopAllAnims()
	elseif movedir.Magnitude == 0 then
		StopAllAnims()
	else
		if Z == 0 then
			anim["backwalk"]:Stop()
			anim["frontwalk"]:Stop()
			anim["frontrun"]:Stop()
		elseif Z < 0 then
			anim["backwalk"]:Stop()
			if not anim["frontrun"].IsPlaying then
				anim["frontrun"]:Play()
			end
			anim["frontrun"]:AdjustWeight(Z*-1.1)
			if not anim["frontrun"].IsPlaying then
				--anim["frontwalk"]:Play()
			end
		--	anim["frontwalk"]:AdjustWeight(1-((Humanoid.WalkSpeed/10)-1))
		elseif Z > 0 then
			if not anim["backwalk"].IsPlaying then
				anim["backwalk"]:Play()
			end
			anim["backwalk"]:AdjustWeight(Z*1.1)
			anim["frontrun"]:Stop()
			anim["frontwalk"]:Stop()
		end
		if X == 0 then
			anim["rightwalk"]: Stop()
			anim["leftwalk"]: Stop()
		elseif X < 0 and not UIS:IsKeyDown(Enum.KeyCode.W) and not UIS:IsKeyDown(Enum.KeyCode.S) then
			if not anim["rightwalk"].IsPlaying then
				anim["rightwalk"]:Play()
			end
			anim["rightwalk"]:AdjustWeight(X*-1.2)
			anim["leftwalk"]:Stop()
			--print(X*-1.2)
		elseif X > 0 and not UIS:IsKeyDown(Enum.KeyCode.W) and not UIS:IsKeyDown(Enum.KeyCode.S) then
			anim["rightwalk"]:Stop()
			if not anim["leftwalk"].IsPlaying then
				anim["leftwalk"]:Play()
			end
			anim["leftwalk"]:AdjustWeight(X*1.2)
			--print(X*1.2)
		elseif X > 0 and Z > 0 then
			anim["rightwalk"]:Stop()
			if not anim["leftwalk"].IsPlaying then
				anim["leftwalk"]:Play()
			end
			anim["leftwalk"]:AdjustWeight(X*0.4)
		elseif X > 0 and Z < 0 then
			anim["rightwalk"]:Stop()
			if not anim["leftwalk"].IsPlaying then
				anim["leftwalk"]:Play()
			end
			anim["leftwalk"]:AdjustWeight(X*0.4)
		elseif X < 0 and Z > 0 then
			if not anim["rightwalk"].IsPlaying then
				anim["rightwalk"]:Play()
			end
			anim["rightwalk"]:AdjustWeight(X*-0.4)
			anim["leftwalk"]:Stop()
		elseif X < 0 and Z < 0 then
			if not anim["rightwalk"].IsPlaying then
				anim["rightwalk"]:Play()
			end
			anim["rightwalk"]:AdjustWeight(X*-0.4)
			anim["leftwalk"]:Stop()
		end
	end
end)

HeadRotation local script :

--Made by @wj1z on 2021. 

--// Services \\--
local replicatedStorage = game:GetService("ReplicatedStorage")
local tweenService = game:GetService("TweenService")
local runService = game:GetService("RunService")
local players = game:GetService("Players")

--// Variables \\--
local remotesFolder = replicatedStorage:WaitForChild("Remotes")
local remotes = {
	HeadRotation = remotesFolder:FindFirstChild("HeadRotation")
}

local camera = workspace.CurrentCamera

local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local rootPart = character:WaitForChild("HumanoidRootPart")
local torso = character:WaitForChild("Torso")
local neck = torso:WaitForChild("Neck")

local neckYOffset = neck.C0.Y

local TweenInfos = {
	NeckInfo = TweenInfo.new (
		0.6,
		Enum.EasingStyle.Quint
	)
}

--// Functions \\--
local function NeckExists()
	local currentHealth = humanoid.Health
	if currentHealth > 0 and camera.CameraSubject == humanoid and rootPart ~= nil and torso ~= nil and neck ~= nil then
		return true
	end
	
	return false
end

local function TweenNeck(targetNeck, targetCFrame)
	tweenService:Create(targetNeck, TweenInfos["NeckInfo"], {C0 = targetCFrame}):Play()
end

--// Events \\--
runService.Stepped:Connect(function()
	if NeckExists() == true then
		local cameraDirection = rootPart.CFrame:ToObjectSpace(camera.CFrame).LookVector
		
		local targetCFrame = CFrame.new(0, neckYOffset, 0) * CFrame.Angles(3 * math.pi / 2, 0, math.pi) * CFrame.Angles(0, 0, -math.asin(cameraDirection.X)) * CFrame.Angles(-math.asin(cameraDirection.Y), 0, 0)
		TweenNeck(neck, targetCFrame)
	end
end)

remotes.HeadRotation.OnClientEvent:Connect(function(otherPlayer, neckCFrame)
	local otherCharacter = otherPlayer.Character
	
	local playerTorso = otherCharacter:FindFirstChild("Torso")
	local playerNeck = playerTorso:FindFirstChild("Neck")
	
	if playerNeck ~= nil then
		TweenNeck(playerNeck, neckCFrame)
	end
end)

--// Loops \\--
while task.wait(0.30) do
	if NeckExists() == true then
		remotes.HeadRotation:FireServer(neck.C0)
	end
end

I also noticed when i look up to the sky, the animations just stop

Animations play relative to existing motor C0/C1 values I believe, so changing those will also impact animation orientation.

I think there’s also a Transform property you can change that does not impact animation orientations (if I’m not mistaken) belonging to motor objects. Maybe try playing around with that instead.