NPC Animation won't stop playing

Hey so I’m making an NPC system, however the run animation keeps playing and won’t stop. Script is below.

local randomNPC = script.Parent

local runAnim = randomNPC.Humanoid:LoadAnimation(randomNPC:FindFirstChild('RunAnim'))
local idleAnim = randomNPC.Humanoid:LoadAnimation(randomNPC:FindFirstChild('IdleAnim'))

randomNPC:FindFirstChild('Humanoid').StateChanged:Connect(function(oldstate,newState)
	if newState ~= Enum.HumanoidStateType.Running then
		local animationTracks = randomNPC:FindFirstChild('Humanoid'):GetPlayingAnimationTracks()
		for _,tracks in pairs(animationTracks) do
			if tracks.IsPlaying == true then
                              tracks:Stop()
                        end
		end
	else
		runAnim:Play()
	end
end)

Any help would be highly appreciated!

You’re only stopping every animation track instance loaded into the NPC’s ‘Humanoid’ instance if the ‘Humanoid’ instance enters a state of ‘Running’.

Play the run animation when the humanoid state is entered and stop the animation when the humanoid state is left.